使用假值结束 do while 循环C++
Ending a do while loop in C++ with a phony value
我很难弄清楚如何在C++中结束do-while循环。我需要在处理 x 数量的数据集后停止循环。也就是说,没有固定的数据量,用户确定何时完成输入值。
我需要能够在用户决定完成输入数据时停止我的 do-while 循环。
我的主程序应该在一个循环中读入和处理 3 个整数值的组,直到数据集结束。
对于每组 3 个值,主程序将打印这些值,然后将这 3 个值作为参数发送到另一个函数。
这是我到目前为止所拥有的:
#include <iostream>
using namespace std;
int main() {
int temp1, temp2, temp3;
do {
cin >> temp1 >> temp2 >> temp3;
cout << "The 3 values are: " << temp1 << " " << temp2 << " " << temp3 << endl;
}
while (****this is where I need help!*****);
return 0;
}
我的问题是我有多个输入值,那么我怎么知道 while 循环应该具有什么条件才能停止处理值?
最简单的解决方案是引入一个新输入,该输入基于值(例如 y 或 n(执行中断操作
char temp4
std::cin>>temp4;
if(temp4!='y')
break; //exits the loop
或在
while(temp4=='y');
如果你想
这样做直到EOF,你需要break
:
do {
cin >> temp1 >> temp2 >> temp3;
if (!cin)
break;
cout << "The 3 values are: " << temp1 << " " << temp2 << " " << temp3 << endl;
}
while (true);
或:
while (true) {
cin >> temp1 >> temp2 >> temp3;
if (!cin)
break;
cout << "The 3 values are: " << temp1 << " " << temp2 << " " << temp3 << endl;
}
相关文章:
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- while循环中while循环的时间复杂度是多少
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 擦除while循环中迭代的元素
- 为什么我不能在 while 循环中创建线程?
- 退出简单while循环时出现问题
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- 嵌套While循环不起作用(C++问题)
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 使用 GMP 的 while 循环出现问题
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 为什么在 while 循环中返回表达式不起作用
- 如何使用 do while 循环确定最高值和最低值
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- OpenCV QT,显示视频的帧(不使用while循环)
- 在 while 循环中使用 pow() 函数 C++
- C++ 制作/命名不初始化的向量(在 while 循环中)
- std::getline没有在while循环中重新请求用户输入