Sign in to follow this  
andyb716

String Concatenation in C++

Recommended Posts

I have a problem concatenating strings. I enter in the name and address and then I want to concatenate them. But I'm trying to do something else. I'm trying to add 123 to the end of "name". but all thats showing up is "name". #include <string> #include <iostream> using namespace std; void getline( std::istream& is, std::string& s, char d = '\n' ) { const int MAX = 128; char getbuff[MAX]; is.getline(getbuff,MAX,d); s = std::string(getbuff,MAX); } int main(){ string name; string address; string all; cout << "Enter your name>" << endl; getline(cin,name); cout << "Enter your address>" << endl; getline(cin,address); all += name; cout << all; all += "123"; cout << all; return 0; }

Share this post


Link to post
Share on other sites
Quote:
Original post by andyb716
#include <string>
#include <iostream>
using namespace std;

void getline( std::istream& is, std::string& s, char d = '\n' )
{
const int MAX = 128;
char getbuff[MAX];
is.getline(getbuff,MAX,d);
s = std::string(getbuff,MAX);
}


That is your problem string already has a "getline" free function, your dumping the whole of std namespace and then overloading getline. Moral of the story don't dump whole namespaces in global namespace!

it should be:

#include <string>
#include <iostream>

int main() {
std::string name;
std::string address;
std::string all;

std::cout << "Enter your name>\n";

std::getline(std::cin, name);

std::cout << "Enter your address>\n";

std::getline(std::cin, address);

all += name;

std::cout << all;

all += "123";

std::cout << all << std::endl;

return 0;
}


Share this post


Link to post
Share on other sites
Now the address won't concatenate with the name, I read some stuff about how getline() has bugs? how do I fix it or what do I need to look for on google?

#include <string>
#include <iostream>

int main(){

std::string name;
std::string address;
std::string all("");

std::cout << "Enter your name>" << std::endl;

std::getline(std::cin,name);

all += name + ",";

std::cout << "Enter your address>" << std::endl;

std::getline(std::cin,address);

all += address;

std::cout << all << std::endl;

return 0;
}

Share this post


Link to post
Share on other sites
I tested your code and it works for me on gcc 3.4, what compiler are you using?

if it's VC++ 6 then downlaod & update it with service packs 5 then 6 to sort *alot* of stuff out.

Share this post


Link to post
Share on other sites
Quote:
Original post by andyb716
Now the address won't concatenate with the name, I read some stuff about how getline() has bugs? how do I fix it or what do I need to look for on google?


The Dinkumware implementation of the STL included with VC 6.0 had a number of bugs including a delimeter problem in std::getline.

The fixes can be found here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this