Skip to main content
gamedev.net
🔒 Locked

getline() problems

Started by codemonkey423 1 reply 1.1k views
Original Post
codemonkey423
codemonkey423
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.
Barius
Barius
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.
codemonkey423
codemonkey423
That was it. That was it exactly. Thanks a lot! It works perfectly now.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.