Jump to content
  • Advertisement
Sign in to follow this  
codemonkey423

getline() problems

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!