Archived

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

c++ string questing

This topic is 5671 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
In your case, you want to read a line. use getline( cin, name );.

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

Edit: corrected the order

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 4, 2002 11:33:34 PM]

Share this post


Link to post
Share on other sites
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
Not cin.getline( char*, size_t ) but std::getline( std::istream&, std::string& ), which is declared in and works on strings.


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 4, 2002 11:33:08 PM]

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