hey all. I've been banging my head on this for hours.

I'm trying to get only whole numbers from a user and if they input anything different, I want it to display a validation error and restart the function. Here's what I have:

I'm either getting an infinite loop of validation errors or it keeps the old cin input. It's getting very frustrating :angry:

void takeAGuess() {

// I have a random number function.
	cout << "\nI'm thinking of a number between 0 and 10..." << endl;
	cout << "\nYour Guess:" << endl;
	
	int userGuess;
	cin >> userGuess;
	while (!cin >> userGuess) {
		cout << "INVALID INPUT.";
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(),'\n');
                takeAGuess();
	}
}

What am I doing wrong?!

I could easily solve this with a Try/Catch block with a NumberFormatException in Java; I'm very new to C++

Recommended Answers

All 7 Replies

I don't see the problem, the code works fine for me.

You can use cin.fail() that is provided in <iostream>

#include <iostream>
using namespace std;

void main(){
int userGuess;
int rightAnswer = rand()%10 + 1;

while(true){
cout << "\nYour Guess: " << endl;
cin >> userGuess
if(cin.fail()){
     cin.clear();
     cin.ignore();
    cout << "Invalid" << endl;
}
if(userGuess == rightAnswer){
     cout << "You win" << endl;
     break;
}
}

cin.fail() will be set if, for instance, you try and cin a character into an int.

I think its hear :

cin >> userGuess;
	while (!cin >> userGuess) {

In there you are asking the user to input a guess first. Then say it failed.
Then in the while loop it goes to ask the user to input a number
again. But the stream is in a failed state so that produces an error.
And since I don't know the operator precedience by head, I am not
sure if the ! gets evaluated before >>.

TO solve your problem change this part :

cin >> userGuess;
	while (!cin >> userGuess)

to this :

//cin >> userGuess;
while (!(cin >> userGuess) ){ ... }

Notice I commented out the cin >> userGuess part, which means you
don't need that.

You can use cin.fail() that is provided in <iostream>

#include <iostream>
using namespace std;

void main(){
int userGuess;
int rightAnswer = rand()%10 + 1;

while(true){
cout << "\nYour Guess: " << endl;
cin >> userGuess
if(cin.fail()){
     cin.clear();
     cin.ignore();
    cout << "Invalid" << endl;
}
if(userGuess == rightAnswer){
     cout << "You win" << endl;
     break;
}
}

cin.fail() will be set if, for instance, you try and cin a character into an int.

This works great but it gives me the other error I stated. It solves the infinite loop but now it holds onto the cin input and the next area I have a different cin validation, it takes it and cause the program to end (as is the intended functionality of the second validation stage.) Maybe it's time for some more code...

void takeAGuess() {
        cout << "\nI'm thinking of a number between 0 and 10..." << endl;
        cout << "\nYour Guess:" << endl;
	int userGuess;
	int difference;
        cin >> userGuess;
	        if (cin.fail()) {
		        cin.clear();
		        cin.ignore();
		        cout << "\nINVALID INPUT." << endl;
		        takeAGuess();
	        } else {
                        cin.clear();
                        //do some work with userGuess
                        continueGame();
                }

void continueGame() {
        cout << "\nContinue?... (Answer with 'Y' to continue)" << endl;
        char answer[4];
        cin.getline(answer, 4);

	// Continue playing
	if (_stricmp("Y", answer) == 0) {
	        // Start game again.
	} else {
		cout << "\n\nThanks for playing the \"Hot or Cold\" game!" << endl;
                // do some "end game work.
		}
}

Thanks again everyone so much for your help! It is sincerely appreciated :)

@firstPerson

I used to have that code and that's how I get in an infinite loop where it constantly spits out "INVALID INPUT."

This isn't what I want...

SOLVED IT!:

added fflush(stdin);

Thanks again everyone for your help! :P

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.