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