我怎样才能在不无限重复的情况下保持循环
How do I stop this from infinitely repeating, but still keep the loop?
在第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
)。
相关文章:
- 如何在不知道C++中有多少可选参数的情况下在循环中使用va_arg?
- 循环依赖,在继承类的情况下使用覆盖方法
- C++在不使用pow或循环的情况下计算一个数字的幂
- 如何在不循环的情况下使用getline读取文件
- 如何在没有同步的情况下使用多个线程(2、4,8、16 个线程)在循环(10,100、1000 个周期)中打印字符串?
- 如何在没有可变参数函数的情况下循环函数的参数
- 如果 return 语句在带括号的循环中,错误可能会到达非 void 函数的末尾,但不会显示在没有括号的循环的情况下
- 在给定相同的输入的情况下,某些代码怎么可能花费更多时间来运行,这似乎只是因为它处于循环中?
- 如何在不中断整个循环的情况下跳过循环交互 - C++
- 如何在不停滞主循环的情况下实现对话系统?
- 如何在不将其拆分为两个嵌套循环的情况下打印整个形状?
- 了解如何在不冻结事件循环的情况下在 QThread 中休眠/等待
- 视觉 在C++中,我试图在没有递归的情况下循环访问许多对象
- 默认情况下,C++ 开关运行时循环
- while 循环是否在设置标志的情况下运行多次?
- 有没有办法在不使用循环的情况下非具体地引用映射中的每个值?C++
- 我可以在不循环访问数组/向量的情况下检查数组/向量中的单词吗?
- 您可以在没有列表或向量的情况下循环访问类对象吗?
- 这段代码如何在没有任何循环语句或'goto'或递归的情况下循环?
- 我如何在没有获得 EOF 的情况下循环通过 ifstream::get