Integer validation for input

前端 未结 3 868
忘掉有多难
忘掉有多难 2020-11-27 21:07

I tried to prompt user for input and do the validation. For example, my program must take in 3 user inputs. Once it hits non-integer, it will print error message and prompt

3条回答
  •  被撕碎了的回忆
    2020-11-27 21:49

    Your question did get myself into other issues like clearing the cin on fail() --

    double read_input()
    {
    double input;
    int count = 0; 
    bool valid = true;
    while(count != 3) {
        cout << "Enter number: " ;
        //cin.ignore(); 
        cin >> input; 
        if(cin.fail())
        {
            cout << "Wrong Input" <::max(), '\n'); 
        }
        else 
                count++;
    }
    return input;
    }
    

提交回复
热议问题