循环时无法中断C++

Cannot break while loop in C++

本文关键字:中断 C++ 循环      更新时间:2023-10-16

我创建了一个程序,用于计算棱镜的体积和矩形的面积。 但我希望用户决定他想使用这个程序多长时间:

#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(如果checky,那么它就不是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