c++ string questing

Started by
8 comments, last by Shenron 21 years, 10 months ago
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;
  
Advertisement
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]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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);

  std::string name;char buffer [256];std::cin.getline (buffer, 256);name = buffer;  
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]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
The strlen() function is a good place to look too, if you ever need to find the length of the string.
Peon
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 */
/*=========================================*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
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.
"after many years of singularity, i'm still searching on the event horizon"
std::cin.getline in VC 6.0 is indeed bugged, which is why I suggested the char buffer hack.
quote:
The getline() in VC6 seems like to eat away two new-line ('\n') instead of one... so beware.


FIX: getline Template Function Reads Extra Character (Q240015)

[edited by - kvh on June 5, 2002 9:04:07 AM]

This topic is closed to new replies.

Advertisement