string name;
cout << "Name :";
cin >> name;
c++ string questing
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.
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]
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]
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);
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]
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]
The strlen() function is a good place to look too, if you ever need to find the length of the string.
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.
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.
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
Popular Topics
Advertisement