-
Problem with cin...
How can I do it so that when I use cin to store an array, it keeps on storing even after a space, so that it is something like this:
Code:
char Name[20];
cin >> Name;
cout << Name << endl;
and lets say I input "Marcos Marin", and then it outputs "Marcos Marin", not just "Marcos". see what I mean?
-
Use getline():
Code:
char name[20];
cin.getline(name, 20, '\n');
-
Now im just a newbie but i dont see the problem since a space is a char just like any other, it doesnt count as a null or anything. So if you type the whole string at once and enter, the if i outputs anything it should output the whole string?:confused:
-
Thanks that worked, but now it seems that I have to press enter twice when I enter something, and its kind of annoying, is there any other way? thaks for your help anyway.
-
Thernkst: For some strange reason it kind of stops stroing data after a space.
-
"Thernkst: For some strange reason it kind of stops stroing data after a space."
It's not strange at all, but that's besides the point. The operator >> is defined to work that way: it ignores any whitespace preceding the input and stops reading input when it encounters a space. So, input like this is all equivalent:
Code:
Mark Sam Joe
Mark Sam Joe
Mark Sam Joe
"...but now it seems that I have to press enter twice when I enter something, and its kind of annoying, is there any other way?"
That means you must be using VC++6.0 as your compiler. Having to hit return twice to enter input when using getline() is a result of a bug in the compiler. You can fix it by changing a line of code in the system file as described here:
http://support.microsoft.com/default...;EN-US;q240015
-
Of course that idiotic bug fix article doesn't tell you what file the code is in. After an exhaustive search, I found it in the file:
C++Microsoft Visual Studio\VC98\Include\String