Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Shenron

c++ string questing

This topic is 6036 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

What can I use to put a sentence in a variable. I tried using this and I put in John G. Smith but I only get John and it skips the rest of the following cins. Is there a function to use when your inputting strings with spaces? Thanks.
  
string name;
cout << "Name :";
cin >> name;
  

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Fruny
In your case, you want to read a line. use getline( name, cin );.

Edit: remember that cin and cout are global objects, not keywords




hmm how exactly do you use getline? i looked it up in my book and it says it has to be passed 2 parameters, the variable and number of characters. i tried all these but i got compile errors on all of them.

cin.getline(name);
cin.getline(name, 256);
cin.getline(name, cin);

Share this post


Link to post
Share on other sites
The strlen() function is a good place to look too, if you ever need to find the length of the string.

Share this post


Link to post
Share on other sites
cin.getline has always given me problems. I avoid it at all costs. My $0.02.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

Share this post


Link to post
Share on other sites
The getline() in VC6 seems like to eat away two new-line (''\n'') instead of one... so beware. Not sure about VC7.

Ah.. and std::string in VC7 have a difference in allocation (using some internal static variable?) when strlen is very small (<10 maybe?)... heard from somewhere else.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!