When you enter a word inside a char x[30] array, for example Max, it's working, but if you enter Max Maximus, it automaticly fills the NEXT cin. How do you fix it?
When you enter a word inside a char x[30] array, for example Max, it's working, but if you enter Max Maximus, it automaticly fills the NEXT cin. How do you fix it?
Operator >> only reads input up to the whitespace. To read everything until [Enter] is pressed use getline.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Beacause a char array cannot accept strings with spaces. The only way is to use a string array with getline or if you use a char array make it an array of char pointers.
Code:char *pData[ 2 ] = { "Hello World!", "C++ Is Cool!" };
Double Helix STL
swgh, this is an array of C-style strings, not a single string. As you can see both individual strings in it do contain spaces without any problem.
(The only character that a C-style string cannot contain is '\0'.)
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Ah ok sorry anon I think i mis-understood
Double Helix STL
So, how is it done? Do you have to use the pointer method?
getline >> x?
getline x?
More like:
Though you may want to consider making x a std::string instead.Code:cin.getline(x, 30);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It just skips the cin when the program is run when you use getline!?
Please post your example. There are minor differences that could cause your problem that depend on the code you are doing.
Using getline will read in multiple words even if there are spaces between them. Using operator>> will read in only a single word.
Using C++ string (i.e. std::string) is smart but has no effect on whether spaces will be read in or not.
Do you have a book or a favorite reference website? It should describe how to call getline.