Public Group

Archived

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

c++ string questing

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

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 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 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 on other sites

  std::string name;char buffer [256];std::cin.getline (buffer, 256);name = buffer;

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 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 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 on other sites
std::cin.getline in VC 6.0 is indeed bugged, which is why I suggested the char buffer hack.

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 10
• 14
• 10
• 25
• Forum Statistics

• Total Topics
632647
• Total Posts
3007640
• Who's Online (See full list)

There are no registered users currently online

×