Sentimental Not Working Correctly
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <iostream>
#include <iomanip>
using namespace std;
void main()
{
cout << endl;
cout << setw(34)<<"Weekly Payroll Report \n";
cout << "------------------------------------------- \n\n";
int EmployeeNum;
double GrossPay , WithHoldings, TotalGrossPay=0 , TotalWithHoldings=0 , TotalNet=0 ;
do
{
cout << "Enter your Employee Number (0 to quit): ";
cin >> EmployeeNum;
while ( EmployeeNum < 0 )
{
cout << "Sorry. Employee Number are positive. Enter again: ";
cin >> EmployeeNum;
}
cout << "Enter gross pay: ";
cin >> GrossPay;
while ( GrossPay <= 0 )
{
cout << "Gross must be larger than 0 \n";
cout << "Enter gross pay: ";
cin >> GrossPay;
}
TotalGrossPay += GrossPay;
cout << "Enter withholdings: ";
cin >> WithHoldings;
while ( WithHoldings > GrossPay )
{
cout << "The Withholding may not be larger than the gross pay. \n";
cout << "Enter withholdings: ";
cin >> WithHoldings;
}
TotalWithHoldings += WithHoldings;
TotalNet = TotalGrossPay - TotalWithHoldings;
}
while ( EmployeeNum != 0);
cout << fixed << setprecision(2) << endl;
cout << "Total Gross Pay: " << setw(15) << "$ " << setw(12) << TotalGrossPay << endl;;
cout << "Total Withholdings: " << setw(12) << "$ " << setw(12) << TotalWithHoldings << endl;
cout << "Total Net: " << setw(21) << "$ " << setw(12) << TotalNet << endl;
}
|
The sentimental value "while (EmployeeNum != 0)" works but only after it does one loop. I don't know why it's doing it. For instance.
Weekly Payroll Report
-------------------------------------------
Enter your Employee Number (0 to quit): 0
Enter gross pay: 2
Enter withholdings: 1
Total Gross Pay: $ 2.00
Total Withholdings: $ 1.00
Total Net: $ 1.00
Press any key to continue . . .
As you can see, it does not terminate right away. It continues to do one loop. Help? Thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
int main(int argc, char *argv[])
{
cout << endl;
cout << setw(34)<<"Weekly Payroll Report \n";
cout << "------------------------------------------- \n\n";
int EmployeeNum;
double GrossPay , WithHoldings, TotalGrossPay=0 , TotalWithHoldings=0 , TotalNet=0 ;
cout << "Enter your Employee Number (0 to quit): ";
cin >> EmployeeNum;
while ( EmployeeNum != 0)
{
while ( EmployeeNum < 0 )
{
cout << "Sorry. Employee Number are positive. Enter again: ";
cin >> EmployeeNum;
}
cout << "Enter gross pay: ";
cin >> GrossPay;
while ( GrossPay <= 0 )
{
cout << "Gross must be larger than 0 \n";
cout << "Enter gross pay: ";
cin >> GrossPay;
}
TotalGrossPay += GrossPay;
cout << "Enter withholdings: ";
cin >> WithHoldings;
while ( WithHoldings > GrossPay )
{
cout << "The Withholding may not be larger than the gross pay. \n";
cout << "Enter withholdings: ";
cin >> WithHoldings;
}
TotalWithHoldings += WithHoldings;
TotalNet = TotalGrossPay - TotalWithHoldings;
cout << "Enter your Employee Number (0 to quit): ";
cin >> EmployeeNum;
}
cout << fixed << setprecision(2) << endl;
cout << "Total Gross Pay: " << setw(15) << "$ " << setw(12) << TotalGrossPay << endl;
cout << "Total Withholdings: " << setw(12) << "$ " << setw(12) << TotalWithHoldings << endl;
cout << "Total Net: " << setw(21) << "$ " << setw(12) << TotalNet << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
|
tested and working
Last edited on
Thank you for that k0t4. (:
I forgot to add that it must be a do-while loop.
bump? It has to be a do-while!
?
Topic archived. No new replies allowed.