-2

I am making a simple guess the number game using C++. My program checks if the user input is an integer or not. But when I input for example "abc" the program keeps saying: "Input a number!" instead of saying it once and let the user input something again..

Code:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int chances = 3;
void ask();
void checkAnswer(int ans);
void defineNumber();
int correctAnswer;

void defineNumber(){
    srand(time(0));
    correctAnswer = rand()%11;
}

void checkAnswer(int ans){
    if(ans == correctAnswer){
        cout << "The answer was right!\n" << endl;
        exit(0);
    }else{
        if(chances > 0){
            cout << "Wrong answer, try again!\n" << endl;
            chances--;
            ask();
        }else{
            cout << "You lost!" << endl;
            exit(0);
        }
    }
}

void ask(){
    int input;
    cout << correctAnswer << endl;
    try{
        cin >> input;
        if(input > 11 || input < 0){
            if(!cin){
                cout << "Input a number!" << endl; //HERE LIES THE PROBLEM
                cin.clear(); //I TRIED THIS BUT DIDN'T WORK AS WELL
                ask();
            }else{
                cout << "Under 10 you idiot!" << endl;
                ask();
            }
        }else{
            checkAnswer(input);
        }
    }catch(exception e){
        cout << "An unexpected error occurred!" << endl;
        ask();
    }
}

int main(){
    cout << "Welcome to guess the number!" << endl;
    cout << "Guess the number under 10: ";
    defineNumber();
    ask();
}

Thanks in advance.

OpenGLmaster1992
  • 281
  • 2
  • 5
  • 13
  • 2
    possible duplicate of [infinite loop with cin](http://stackoverflow.com/questions/5864540/infinite-loop-with-cin) – user4581301 Sep 22 '15 at 15:34
  • No, because Smeilliz his answer is different from the others in that question. – OpenGLmaster1992 Sep 22 '15 at 15:48
  • And at the time the answer was incorrect. Now that it isn't, the two important points,`cin.clear();` and `cin.ignore(std::numeric_limits::max(), '\n');`, are identical. – user4581301 Sep 22 '15 at 18:57

1 Answers1

0

Try this:

try{
    cin >> input;
    if (cin.good()) {
      if(input > 11 || input < 0) {
        cout << "Under 10 you idiot!" << endl;
        ask();
      } else {
        checkAnswer(input);
      }

    } else {
      cout << "Input a number!" << endl;
      cin.clear();
      cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      ask();
      }

}catch(exception e){
    cout << "An unexpected error occurred!" << endl;
    ask();
}

and don't forget to use this at the beginning: #include <climits>

The cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); line will ignore everything until the next int number.. Therefore it will not loop anymore..

Tokay
  • 157
  • 2
  • 14
  • 1
    `clear()` clears the error condition, but the "abc" is still there in the buffer and will cause a new error for the next input. Check the function `ignore` for skipping input. – Bo Persson Sep 22 '15 at 15:32
  • But I need to let the user input again, when the answer is wrong. That's why I called the ask function again, I hoped that the input would have been "resetted". Any idea how to fix this? – OpenGLmaster1992 Sep 22 '15 at 15:39
  • `cin.ignore(std::numeric_limits::max(), '\n');` is a bit more portable, IMHO – YePhIcK Sep 22 '15 at 15:51