getline() problems

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

So I'm messing around trying to make a very simple console-like parser-thing and was trying to use getline() to get text from the user. Well, it'll be easier if I post the code.
while(isRunning != false)
{
getline(cin, command); // get the command from the user

if(command == "help")
{
// do "help" here
}
else if(command == "quit" || command == "exit")
{
isRunning = false;
}
else
{
cout << "Please enter a valid command. To see a list of available commands, type help.\n" << endl;
}
}

The problem is, the program runs the things under "else" as soon as it enters the while loop; it doesn't seem to wait for the first "getline." Why is this? Thanks a lot for the help.

Share on other sites
Are you using other input methods like "cin >> variable" before this piece of code? If so, there might still be the rest of a line in the input stream, which std::getline then grabs.

Share on other sites
That was it. That was it exactly. Thanks a lot! It works perfectly now.

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
A4L
13
5. 5

• 12
• 16
• 26
• 10
• 44
• Forum Statistics

• Total Topics
633767
• Total Posts
3013737
×