0

I created a program, that calculates volume of prism and area of rectangle. But I want user to decide how how long he wants to work with this programm:

#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;
}

In the first part everything works fine, but I cannot get out of this loop:

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

Where did I do the mistake and how can I fix it?

2
  • 2
    if you read your while-condition out loud, you'll find your logic error --> no matter what you enter, it always evaluates as true. Commented Oct 13, 2017 at 12:39
  • I just realised it is a nested meme loop. Creating a function, running it first time on default and then having a switch case wouldn't be better? Commented Oct 13, 2017 at 13:12

7 Answers 7

7

while (run = true) is always true, and it has the side effect of setting run to true.

Did you want ==? Didn't your compiler warn you of this? Better still, drop the tautology run == true and write while (run).

Then fix the other conditional to while (check != 'y'&& check != 'n'). Think about this: check != 'y'|| check != 'n' is always true (if check is y, say, then it's therefore not n). That logic error is extremely common.

Finally, I'm not sure what you really achieve with the introduction and therefore the maintenance of run. Why not replace it with while (true), and write return 0; in place of break?

Sign up to request clarification or add additional context in comments.

Comments

4

You did 2 mistakes :

1 : while(run = true){ is an assignement. Use == to compare

2: while(check != 'y'|| check != 'n'){

So if check is different than y (lets say check = 'n'), you will continue your loop. Use && instead

Here is a working example :

#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;
}

5 Comments

cout messages are repeating without adding cin.clear(); and consider removing one of those cout << "Do you want to try again?(y/n)\n"; , cout <<"Please enter y or n\n";
@AESTHETICS This is not relevant to his question
It is related to aesthetics of the question. Output of the program might not be clear and by that answer might be confusing.
@AESTHETICS thus your username haha
For better understanding you can also use (!(check == 'y' || check =='n')) for the while statement
2

Here is how it should be done:

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

Full code:

#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;
}

2 Comments

It only fixes half the issues: the program still runs for ever.
What do you think while(run = true){ does?
1
check != 'y' || check != 'n'

This is always true, since check cannot equal to 'y' and 'n' at the same time. You should change it to

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

By the way, you wrote at the outer loop

while (run = true)

This will result in run getting assigned to true at the begining of each outer loop and since this expression also returns true, you end up in a dead loop. This should be changed to

while (run == true)

Enabling compiler warning and possibly -Werror will help avoid such problem. And of course for bool variables you can just write while (run).

1 Comment

Could I suggest you use while (run) instead of while (run == true)? Then there's no confusion about whether it's = or ==, and I think it looks a bit neater.
1

For starters the variable run in this loop

bool run = true;
while(run = true){
    // ...
    if(check == 'n'){
        run = false;
        break;
    }
}

is redundant. In fact the loop does not depend on the variable run. The loop stops its iteration due to the break statement in the if statement

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

independent on whether the Boolean literal true will be assigned to the variable as it is done in the condition of the while statement (it seems you mean comparison run == true instead of the assignment)

while(run = true){

or the Boolean literal false will be assigned to the variable as it is done in the if statement

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

In fact you have an infinite loop that can be stopped only by the break statement.

So just remove the variable and write the loop the following way

while( true ){
    // ...
    if(check == 'n'){
        break;
    }
}

As for the second loop

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

then its condition will always yield true because neither character can be simultaneously equal to 'y' and to 'n'.

So a valid condition should look like

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

Or you could make it even more readable the following way

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

Comments

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;
}

You must change the = sign in the first loop for == and also need &&instead of ||in the other while loop.

Comments

-1

Try having check as a string instead of a char.

2 Comments

This is about as useful as a trap door in a canoe.
Hm, why? @Bathsheba

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.