Jump to content
  • Advertisement
Sign in to follow this  
andyb716

String Concatenation in C++

This topic is 5086 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!