0

Why does this thing keep looping ? .. I tried clearing the cin stream but still it loops if i enter something other than a number.

void AskQuestion()
{
    DisplayQuestion();
    bool Exit = false;
    int input = 0;
    cout<<"Input : ";
    if(!(cin>>input))
    {
        cout<<"Invalid Input"<<endl;
        cin.ignore(10000,'\n');
        cin.clear();
    }
    else
    {
        Exit = ProcessInput(input);
    }
    if(!Exit) AskQuestion();
}
4

1 に答える 1

2

まず、再帰を使用しないでくださいwhile

次に、同期を使用します。

int number;
while (true) {
    if (cin >> number)
        break;
    cout << "Invalid number.\n";
    cin.sync();
    cin.clear();
}
cout << "Number: " << number;
于 2012-06-10T06:28:40.323 に答える