循环时无法中断C++
Cannot break while loop in C++
我创建了一个程序,用于计算棱镜的体积和矩形的面积。 但我希望用户决定他想使用这个程序多长时间:
#include <iostream>
using namespace std;
int main()
{
bool run = true;
while(run = true){
double len,width,area,volume,height;
char check;
cout << "Please, enter length,width and height of a prism(rectangular)!";
cin >> len >> width >> height;
area = width*len;
volume = area*height;
cout << "The area of rectangle is equal to: " << area << "n" << "The volume of rectangular prism is equal to: " << volume << "n";
cout << "Do you want to try again?(y/n)n";
cin >> check;
while(check != 'y'|| check != 'n'){
cout <<"Please enter y or nn";
cin >> check;
}
if(check == 'n'){
run = false;
break;
}
}
return 0;
}
在第一部分中一切正常,但我无法摆脱这个循环:
while(check != 'y'|| check != 'n'){
cout <<"Please enter y or nn";
cin >> check;
}
我在哪里犯了错误,我该如何修复它?
while (run = true)
总是true
,并且它具有将run
设置为true
的副作用。
你想要==
吗?你的编译器没有警告你这一点吗?更好的是,放弃重言式run == true
并写while (run)
.
然后将另一个条件修复为while (check != 'y'&& check != 'n')
.想想看:check != 'y'|| check != 'n'
总是true
(如果check
是y
,那么它就不是n
)。这种逻辑错误非常普遍。
最后,我不确定您通过介绍以及维护run
真正实现了什么.为什么不用while (true)
代替它,并用return 0;
代替break
?
你犯了 2 个错误:
1 :while(run = true){
是分配。使用==
进行比较
2:while(check != 'y'|| check != 'n'){
因此,如果check
与 y 不同(假设check = 'n'
),您将继续循环。请改用&&
这是一个工作示例:
#include <iostream>
using namespace std;
int main()
{
bool run = true;
while (run) {
double len, width, area, volume, height;
char check;
cout << "Please, enter length,width and height of a prism(rectangular)!";
cin >> len >> width >> height;
area = width*len;
volume = area*height;
cout << "The area of rectangle is equal to: " << area << "n" << "The volume of rectangular prism is equal to: " << volume << "n";
cout << "Do you want to try again?n";
do {
cout << "Please enter y or nn"; cin >> check;
cin.clear();
} while (check != 'y' && check !='n');
if (check == 'n') {
run = false;
}
}
return 0;
}
以下是应该如何完成:
do {
cout << "Do you want to try again?(y/n)n"; cin >> check;
cin.clear();
} while (check != 'y' && check !='n');
完整代码:
#include <iostream>
using namespace std;
int main()
{
bool run = true;
while (run) {
double len, width, area, volume, height;
char check;
cout << "Please, enter length,width and height of a prism(rectangular)!";
cin >> len >> width >> height;
area = width*len;
volume = area*height;
cout << "The area of rectangle is equal to: " << area << "n" << "The volume of rectangular prism is equal to: " << volume << "n";
do {
cout << "Do you want to try again?(y/n)n"; cin >> check;
cin.clear();
} while (check != 'y' && check !='n');
if (check == 'n') {
run = false;
}
}
return 0;
}
check != 'y' || check != 'n'
这总是正确的,因为检查不能同时等于"y"和"n"。您应该将其更改为
check != 'y' && check != 'n'
顺便说一句,你在外循环写
while (run = true)
这将导致在每个外部循环的开头将 run 分配给 true,并且由于此表达式也返回 true,因此最终会进入死循环。这应该改为
while (run == true)
启用编译器警告和可能的 -Werror 将有助于避免此类问题。当然,对于布尔变量,您可以编写while (run)
.
对于初学者来说,变量run
在这个循环中
bool run = true;
while(run = true){
// ...
if(check == 'n'){
run = false;
break;
}
}
是多余的。实际上循环不依赖于变量run
。由于 if 语句中的break
语句,循环停止其迭代
if(check == 'n'){
run = false;
break;
}
独立于布尔文字true
是否会像在 while 语句的条件中那样分配给变量(似乎您的意思是比较run == true
而不是赋值)
while(run = true){
或者布尔文字false
将分配给变量,就像在 if 语句中所做的那样
if(check == 'n'){
run = false;
break;
}
事实上,你有一个无限循环,只能通过 break 语句停止。
因此,只需删除变量并按以下方式编写循环
while( true ){
// ...
if(check == 'n'){
break;
}
}
至于第二循环
while(check != 'y'|| check != 'n'){
cout <<"Please enter y or nn";
cin >> check;
}
那么它的条件将始终为 true,因为两个字符都不能同时等于'y'
和'n'
。
所以一个有效的条件应该看起来像
while(check != 'y' && check != 'n'){
cout <<"Please enter y or nn";
cin >> check;
}
或者你可以通过以下方式使其更具可读性
while( not ( check == 'y' || check == 'n' ) ){
cout <<"Please enter y or nn";
cin >> check;
}
#include <iostream>
using namespace std;
int main()
{
bool run = true;
while(run == true){
double len,width,area,volume,height;
char check;
cout << "Please, enter length,width and height of a prism(rectangular)!";
cin >> len >> width >> height;
area = width*len;
volume = area*height;
cout << "The area of rectangle is equal to: " << area << "n" << "The volume of rectangular prism is equal to: " << volume << "n";
cout << "Do you want to try again?(y/n)n";
cin >> check;
while(check != 'y' && check != 'n'){
cout <<"Please enter y or nn";
cin >> check;
}
if(check == 'n'){
run = false;
break;
}
}
return 0;
}
您必须更改第一个循环中的=
符号以进行==
,并且还需要&&
而不是另一个 while 循环中的||
。
尝试将check
作为string
而不是char
。
- 类成员和中断
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 我的代码运行良好,但在游戏循环中中断
- 使用带有MCP23017的 pigpio 进行中断读取
- 为什么我在 AVR 中的中断无法正常工作?
- 使用 UDP 中断 while()-循环
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 如何在 CompleteAsyncIO 中访问 IOMemoryBufferDescriptor,该描述符通过中断 EP
- 如何为伺服电机创建中断或返回值?
- 开关:无外壳中断
- 如何使用中断停止循环?
- 为什么当我输入一个被接受的数字时,我的 do-while 循环没有中断?
- 在 Ubuntu 中编译 SuiteSparse,在安装 CHOLMOD 时中断
- 在PROGMEM中添加更多数据会中断Arduino Mega 2560上的SPI传输
- 附加到 gdb 会中断并且不会继续该过程
- 使用 shared_ptr 在中断时结束多线程循环
- 为什么STM32不中断?
- 如何使用 SIGINT 在第二个线程中断 getchar
- 使用个人C++库编译代码时,与头文件一起使用时会中断
- 为什么我的 while 循环对于特定输入中断,而对于其他输入工作正常?