在C ++中无法打破while循环

时间:2017-10-13 12:34:26

标签: 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 n\n";
            cin >> check;
        }
        if(check == 'n'){
            run = false;
            break;
        }
    }
    return 0;
}

在第一部分中,一切正常,但我无法摆脱这个循环:

while(check != 'y'|| check != 'n'){
                cout <<"Please enter y or n\n";
                cin >> check;
            }

我在哪里犯了错误,如何解决?

7 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:4)

你犯了两个错误:

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 n\n"; cin >> check;
            cin.clear();
        } while (check != 'y' && check !='n');

        if (check == 'n') {
            run = false;
        }
    }
    return 0;
}

答案 2 :(得分:2)

以下是应该如何做的:

jq --argfile A A.json 'map((.type as $t|$A[]|select(.type==$t)//{tag:"NEW"}) + .)' B.json

完整代码:

do {
    cout << "Do you want to try again?(y/n)\n"; cin >> check;
    cin.clear();
} while (check != 'y' && check !='n');

答案 3 :(得分:1)

check != 'y' || check != 'n'

这一切都是正确的,因为支票不能等于&#39; y&#39;和&#39; n&#39;同时。你应该把它改成

check != 'y' && check != 'n'

顺便说一句,你在外循环中写道

while (run = true)

这将导致在每个外部循环开始时将运行分配为true,并且由于此表达式也返回true,因此最终处于死循环。这应该改为

while (run == true)

启用编译器警告以及可能的-Werror将有助于避免此类问题。当然,对于bool变量,您只需编写while (run)

答案 4 :(得分:1)

对于初学者来说,此循环中的变量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 n\n";
        cin >> check;
    }

那么它的条件总是会产生真值,因为这两个字符都不能同时等于'y''n'

因此有效条件应该是

    while(check != 'y' && check != 'n'){
        cout <<"Please enter y or n\n";
        cin >> check;
    }

或者你可以通过以下方式使其更具可读性

    while( not ( check == 'y' || check == 'n' ) ){
        cout <<"Please enter y or n\n";
        cin >> check;
    }

答案 5 :(得分:0)

#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 n\n";
        cin >> check;
    }
    if(check == 'n'){
            run = false;
            break;
            }
    }
    return 0;
}

您必须在=的第一个循环中更改==符号,并在另一个循环中更改&&而不是||

答案 6 :(得分:-1)

尝试将check作为string而不是char