-
Switch??
Hi,
I was wondering why this part of a switch function does not pause to get user input.
Code:
switch(input)
{
case 1:
cout << endl << "CREATE YOUR CHARACTER" << endl;
cout << "------------------------------" << endl << endl;
cout << "Enter your player's name: ";
getline(cin, mName);
cout << endl << endl;
break;
Can someone please provide some enlightenment. Thanks very much. I appreciate it.
-
It has nothing to do with switch, and all to do with "how input works". I'm pretty sure that somewhere shortly before the switch(input) line, you have something lileThis, like scanf() in C, will leave a newline in the input stream. You need to read that newline out of the input stream before you do getline() [since getline will read everything up to the next newline, and if there is already a newline in the input stream, it will say "Ah, ok, you didn't want anything else in the strign, then..."].
Code:
cin.ignore(1000, '\n');
will do that.
Edit: Note, if someone fell asleep on the spacebar key and there are 1000+ spaces after the 1, then it will still fail. But I think that is unlikely enough to not worry about today - someone will surely show us how you make it "the largest ever number".
--
Mats