Reputation: 318
I am using Microsoft Visual studio 2013
I currently have this switch statement:
switch (option)
{
case 1:
getline(cin, newname);
cout << "What would you like your new username to be?\nName: ";
getline(cin, newname);
name = newname;
cout << "\nYour username is now '" << name << "' with your balance being $" << balance << "\n";
options();
break;
case 2:
cout << "\nOpenning cheat menu\n";
cheats();
break;
default:
cout << "\nExitting to main title...\n";
title();
break;
}
you can just ignore the case 2 and default, just showing so you know that those cases work. Case 2 and default work perfectly fine but case 1 is acting weird. As you can see on case 1 I have two getline(cin, newname); You may think I should only have one, but for some reason I need two for this code to function correctly.
If I only have one of these the code jumps past the input the user should give. If I have two it works normally.
I have asked someone this and they do not understand either, so therefore: Why does this work.
Upvotes: 1
Views: 89
Reputation: 3
I think a similar problem has been posted here.
You need to flush the newline with cin.ignore() or cin.sync().
The first call also seems misplaced...
Upvotes: 0
Reputation: 145389
Apparently you have leftover input including a newline, in the input buffer of cin
. That happens when you use other input operations than getline
. They generally don't consume everything in the input buffer.
Instead of the extra getline
you can use ignore
, but better, do that at the place where you earlier do the inputting.
Upvotes: 8