Reputation: 9
cin >> Input; {
if(Input == "a")
Position = "first";
else if(Input == "b")
Position = "second";
else if (Input == "c")
Position = "third";
else if(Input == "Stop")
break;
}
After cin >> Input I have two more cin sections. For some reason it skips over the getline for Name. After I cin >> Input it shows Name: Status: and then does the getline for status.
cout << "Name: ";
getline(cin, Name);
if(Name == "Stop")
break;
cout << "Status: ";
getline(cin, Status);
if(Status == "Stop")
break;
Upvotes: 0
Views: 409
Reputation: 28424
The problem is that most of the input operations will leave newline character in the input buffer (but not the getline()
), more info is here.
In your case this line:
cin >> Input; {
will leave /n
behind, which is feed later to:
getline(cin, Name);
Quick solution:
Replace cin >> Input;
with getline(cin, Input);
Upvotes: 1
Reputation: 12225
I think it should be something like this:
cin.getline(whatever);
cin.ignore(1, '\n');
See, when you've read some text from stdin, string end is determined by \n
(by default). And when you hit Return in your console window, this symbol is skipped, but it is present. So, when you are trying to read the next string, it meets previously forgotten \n
and terminates reading returning an empty string.
Read this for details.
Upvotes: 1
Reputation: 40849
cin >> somevar
leaves behind a newline. No matter what type somevar
is, even if it's a std::string, the end of the line character is not read by that statement. Thus when you get to the name part you read the end of the line for the somevar
line, which is apparently empty. Toss a getline in before the attempt to read name and it should be fine.
Upvotes: 1