• Advertisement
Sign in to follow this  

cin - back to basics

This topic is 3765 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

Hey there. So, I never did fix a little annoyance I had with cin: the fact that if you have say
std::cin >> myInteger;

it just moves to the next line of the console with no complaints at all, and sits there waiting. I'm currently using some code to deal with unexpected input which goes:
char inputBuffer[20];
std::cout << "Enter price: ";
	valid = false;
	while (!valid)
	{
		std::cin >> inputBuffer;
		price = (float)atof(inputBuffer);
		if (price > 0)
		{
			valid = true;
		}
		else
		{
			std::cout << "You must enter a positive non-zero number for price.  Please try again." << std::endl;
			std::cout << "Enter price: ";
		}
	}

but this doesn't solve the bug outlined above. Does anyone have a workaround?

Share this post


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

  • Advertisement