while循环用于输入验证

时间:2014-12-11 23:17:34

标签: c++ loops input while-loop verification

我有一个输入验证的快速问题。我有我的循环,它适用于负数,但是当我输入类似+符号的东西时,它会导致无限循环。有什么办法可以解决吗?

cout << "Input Quarter 1 Sales: " << endl;
    cin >> quarter1;

    while (quarter1 < 0)                                                                                    
    {
        cout << "Error! Sales Must be a Positive Number!" << endl;
        cout << "Please Input a Positive Number" << endl << endl;
        cin >> quarter1;
    }

0 个答案:

没有答案