So i read the "Flush Cin" post at the top of the thread and it just flew right over my head. I have a simple if statement that uses int values to determine where to send the user. If the user enters a char value it shoots itself into an infinite loop... this is my code, please help and thanks =)

int game()
{
	int guess;
	
	system("cls");
	cout << endl << "Start!" << endl;

	srand(time(0));
	int randomNumber = rand() % 100 + 1; // generate random number
	int NUM = (randomNumber); // number between 1 and 100

	bool Wdone = false;
	
do
{
	cout << endl << "Your guess\n- ";
	num_of_guesses++;
	cin.clear();
		cin >> guess;
		system("cls");

		if (guess == randomNumber)
		{
			cout << endl << "You got it! Congrats!" << endl;
			Wdone = true;
			break;
		}
		else if (guess >= randomNumber)
		{
			cout << endl << "Too High!" << endl;
			current_score--;
			Wdone = false;
			continue;
		}
		else if (guess <= randomNumber)
		{
			cout << endl << "Too Low!" << endl;
			current_score--;
			Wdone = false;
			continue;
		}

} while (Wdone == false);

Recommended Answers

All 5 Replies

I don't see any char variables, so I presume you're trying to read a char into line 19, which asks for an int? The thread pinned to the top doesn't really address that issue. If you want to take characters, you'll need to read it into a string, check to make sure the string's a valid number, then if it is, convert it to an integer and if not, give out an error message.

At least that's what I assume you want to do. If so, there are a few ways to do it. Look at the atoi function. That will convert it for you. You'll also need a way to test things. Consider using isdigit from the cctype library for that.

string input;
cin >> input;
// test input for only digits using isdigit and a loop.
// if valid, convert using atoi.
// if not, give an error message and tell them to try again.

I don't see any char variables, so I presume you're trying to read a char into line 19, which asks for an int?

No, he wants to read an int but a non-int does it's looping thing.

You need to read the input as a string and after you've verified that digits were entered convert the string into an integer.

No, he wants to read an int but a non-int does it's looping thing.

You need to read the input as a string and after you've verified that digits were entered convert the string into an integer.

Cool, yeah thats my problem... how would i do that? what would i google?

Cool, yeah thats my problem... how would i do that? what would i google?

Umm,

You need to read the input as a string and after you've verified that digits were entered convert the string into an integer.

In other words

1) You need to read the input as a string - you better not say you don't know how...

2) after you've verified that digits were entered - loops and IFs

3) convert the string into an integer.

Umm,

In other words

1) You need to read the input as a string - you better not say you don't know how...

2) after you've verified that digits were entered - loops and IFs

3) convert the string into an integer.

Alright, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.