#problem with beginner code

3 messages · Page 1 of 1 (latest)

broken thunder
#

ok so I am trying to make a small program for class where the user enters 5 numbers then once they are done the program will display " * " for the amount of each number he entered
I have gotten the program to accept the 5 numbers from the user but i think its not going anywhere after that
here is the code

done = false;
    cout << "\n5.16\n";
    cout << "Bar-Chart Printing Program\n";
    cout << "Enter 5 diffrent numbers from 0-30 (-1 to end)\n";
    int numb1, numb2, numb3, numb4, numb5;
    cin >> numb1;
    cin >> numb2;
    cin >> numb3;
    cin >> numb4;
    cin >> numb5;
    if (numb1 || numb2 || numb3 || numb4 || numb5 == -1)
        done = true;
    while (done == false) {
        for (int number = 0; number < numb1; number++)
        {
            cout << "*";
        }
        cout << "\n";
        for (int number = 0; number < numb2; number++)
        {
            cout << "*";
        }
        cout << "\n";
        for (int number = 0; number < numb3; number++)
        {
            cout << "*";
        }
        cout << "\n";
        for (int number = 0; number < numb4; number++)
        {
            cout << "*";
        }
        cout << "\n";
        for (int number = 0; number < numb5; number++)
        {
            cout << "*";
        }
        cout << "\n";
        cout << "Enter 5 diffrent numbers from 0-30 (-1 to end)\n";
        cin >> numb1 >> numb2 >> numb3 >> numb4 >> numb5;
        if (numb1 || numb2 || numb3 || numb4 || numb5 == -1)
            done = true;
    }
thorn jetty
#

You can't chain logical operators like that.

numb1 || numb2 || numb3 == -1

For example that's checking if numb1 has a non-zero value or numb2 has a non-zero value or numb3 is -1. Only the last one is being compared to -1. To fix it you need to repeat the -1 comparison on each variable in the if statement.

#

Your while also will not run at all. A while loop runs while its condition is true. Your condition is done == false, on the line immediately before you set done to true which is not false making the condition false causing the loop to never run.