我怎样才能在不无限重复的情况下保持循环

How do I stop this from infinitely repeating, but still keep the loop?

本文关键字:情况下 循环 无限      更新时间:2023-10-16

在第33行,有一个中断来阻止代码无限期地重复,但我希望它参加while loop

代码:

#include <iostream>
using namespace std;
int main()
{
while (true){
    {
        cout << "This program counts by twos to any number that is inputted by the user." << endl;
        cout << "Input an even number to start counting." << endl;
        int input;
        cin >> input;
        if (!cin.fail())//fails if input is not an integer
        {
            if (input < 0)//makes sure all numbers are positive
            {
                cout << "That is not a positive number. Try again?" << endl;
            }
            else if (input % 2 != 0) // makes sure all numbers are even
            {
                cout << "That is not an even number. Try again?" << endl;
            }
            else{
                for (int i = 0; i <= input; i += 2) //uses a for loop to actually do the counting once you know that the number is even.
                {
                    cout << i << endl;
                }
            }
        }
        if (cin.fail())//returns this when you input anything other than an integer. 
        {
            cout << "That is not a digit, try again." << endl;
            break;
        }
    }
}
return 0;
}

如果你们能帮我找出为什么这个重复,那就太有帮助了

您需要在for循环之后添加一个break语句,以便退出循环。如果没有break, for循环将执行并打印输出,然后控制将下降到while循环的末尾,它将从循环的顶部开始。

我还建议将if (cin.fail())更改为else,因为您已经检查了if (!cin.fail())。如果想再次循环,还需要忽略其余的输入并清除错误标志。

在while循环中还有一组额外的括号。通过这些更改,您的代码将是:

#include <iostream>
#include <limits>
using namespace std;
int main()
{
    while (true)
    {
        cout << "This program counts by twos to any number that is inputted by the user." << endl;
        cout << "Input an even number to start counting." << endl;
        int input;
        cin >> input;
        if (!cin.fail())//fails if input is not an integer
        {
            if (input < 0)//makes sure all numbers are positive
            {
                cout << "That is not a positive number. Try again?" << endl;
            }
            else if (input % 2 != 0) // makes sure all numbers are even
            {
                cout << "That is not an even number. Try again?" << endl;
            }
            else{
                for (int i = 0; i <= input; i += 2) //uses a for loop to actually do the counting once you know that the number is even.
                {
                    cout << i << endl;
                }
                break;  // exit the while loop
            }
        }
        else //else when you input anything other than an integer. 
        {
            cout << "That is not a digit, try again." << endl;
            cin.clear();  // reset the error flags
            cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n'); // clear any extra input
        }
    }
    return 0;
}

根据您打印的错误消息,我猜您的问题是您想让用户有机会再次尝试输入一个数字,但是在一次失败之后,无论您输入什么,它都继续失败。如果是这种情况,将break替换为cin.clear()。这将告诉流,您已经从错误中恢复,并准备接收更多的输入。

如果要这样做,那么程序现在没有退出条件,因此需要在for循环之后添加break(或return 0)。

相关文章: