My while loop should terminate when the user types 0 for employee number. Unfortunately when you press 0 the loops iterates at least once before terminating. I was under the impression that as soon as the sentinal value is triggered the loop would terminate immediately.

Right now I run program and it asks for employee number, and works fine except if I type 0 for employee number it still executes the subsequent commands one more time before exiting. Please let me know what's going on, the program isn't important but fully understanding the while loop is. And light shed on this would be much appreciated.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
// ****** VARIABLES ************************************

    int employeeNumber = 5;
    double runningGross = 0;
    double runningState = 0;
    double runningFederal = 0;
    double runningFica = 0;
    double runningNet = 0;

// ****** START LOOP **********************************

// Outter while loop

    while(employeeNumber != 0)
    {
        cout << "\nEmplyee Number: ";
        cin >> employeeNumber;
        cout << endl;

        double gross = -1;
        double state = -1;
        double federal = -1;
        double fica = -1;
        double net = -1;

        cout << "\nGross pay: ";
        cin >> gross;
        cout << "\nState Tax: ";
        cin >> state;
        cout << "\nFederal Tax: ";
        cin >> federal;
        cout << "\nFICA: ";
        cin >> fica;

// Validate data
        while(gross <= 0 && state <= 0 && federal <= 0 && fica <= 0)
        {
            cout << "\nOnly positive values are excepted. Please re-enter.\n";
            double gross = -1.5;
            double state = -1;
            double federal = -1;
            double fica = -1;
            double net = -1;

            cout << "\nGross pay: ";
            cin >> gross;
            cout << "\nState Tax: ";
            cin >> state;
            cout << "\nFederal Tax: ";
            cin >> federal;
            cout << "\nFICA: ";
            cin >> fica;

        }

// Calculate running totals
        runningFederal += federal;
        runningFica += fica;
        runningGross += gross;
        runningState += state;

        net = gross -(federal + fica +state);

    }

// Output
    cout << "\n\n************************************************\n\n";
    cout << "\n Total Gross $" << runningGross;
    cout << "\n Total Federal $ " << runningFederal;
    cout << "\n Total State $ " << runningState;
    cout << "\n Total FICA $ " << runningFica;
    cout << "\n Total Net $ " << runningNet << endl << endl;


    return 0;
}

If you insists on breaking out of the loop immediately then you should use break.

Something like this...

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int employeeNumber = 0;

    while( true )
    {
        cout << "\nEmplyee Number: ";
        cin >> employeeNumber;
        cout << endl;

        if ( employeeNumber == 0 ) break;

        while( true )
        {
            cout << "Inner loop\n";
            //do something here
        }
    }
    cout << "Done" << endl;
    return 0;
}

Whoops - sorry - your code didn't load until I refreshed the page, this makes sense! Thank you!

Also, so no matter what, the block of commands will all execute in a while loop whether or not the sentinal value is triggered? I really want to understand the nuts bolts of the loop. Thanks for your reply!

The condition is checked at the start of the while loop. Then the contents of the while loop are executed. All of them.

Changing the value of that condition during the loop has no effect until the condition is checked. When is the condition checked? At the start of the while loop.

Got it! Thanks!

The error in your code is that in lines 46 to 50, you redeclare a new set of variables (gross, state, federal...) which are local to the inside of the while loop. The while loop condition, however, is outside of that scope, meaning that it is referring to the variables declared prior to the loop (at lines 27-31). In C/C++, the rule is that invoking variables (like at lines 52-59) always refers to the variables with those names in the narrowest scope, meaning that those statements simply enter values into those inside-the-loop variables, and does not change the values of the outside-the-loop variables with the same names. And thus, those variables never change value, meaning that the condition never changes, and the loop never ends.

You should simply remove lines 46 to 50, and it should work as expected.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.