Hello all. I have put together a program that will calculate gross pay for an hourly or a salaried employee. I am having a bit of trouble with my validation loop
#include<iostream>
#include<iomanip>
usingnamespace std;
union PaySource
{
int hours;
double salary;
};
int main()
{
PaySource employee;
char payType;
double payRate, bonus, grossPay;
cout<<fixed<<showpoint<<setprecision(2);
cout<<"\tThis program calculates either hourly or salary wages.\n";
cout<<"\t======================================================\n";
cout<<"\n";
cout<<"\tEnter H for hourly wages or S for salary wages: ";
cin>>payType;
cout<<"\n";
while (!(payType =='H') || !(payType == 'h') || !(payType == 'S') || !(payType == 's'))
{
cout<<"\tThe wrong input was made.\n";
cout<<"\tEnter H for hourly wages or S for salary wages: ";
cin>>payType;
cout<<"\n";
}
if (payType =='H' || payType =='h')
{
cout<<"\tWhat is the hourly pay rate for this employee?\n";
cout<<"\tPay rate per hour:$";
cin>>payRate;
cout<<"\n";
cout<<"\tHow many hours has this employee worked?\n";
cout<<"\tHours worked:";
cin>>employee.hours;
cout<<"\n";
while(employee.hours > 80 || employee.hours < 0)
{
cout<<"\tThe wrong input was made.\n";
cout<<"\tPlease enter a positive number of hours less than 80: ";
cout<<"\tHours worked:";
cin>>employee.hours;
cout<<"\n";
}
grossPay = employee.hours * payRate;
cout<<"The gross pay for this employee is: $"<<grossPay<<endl;
}
elseif (payType =='S' || payType =='s')
{
cout<<"\tWhat is the salary rate for this employee?\n";
cout<<"\tSalary pay rate:$";
cin>>employee.salary;
cout<<"\n";
while (employee.salary<0)
{
cout<<"\tThe wrong input was made.\n";
cout<<"\tPlease enter a positive number for the salary rate: ";
cout<<"\tSalary pay rate:$";
cin>>employee.salary;
cout<<"\n";
}
cout<<"\tIs there any bonus this pay period for this employee?\n";
cout<<"\tBonus rate:$";
cin>>bonus;
cout<<"\n";
while (bonus<0)
{
cout<<"\tThe wrong input was made.\n";
cout<<"\tPlease enter 0 or a positive number for the bonus this pay period: ";
cout<<"\tBonus rate:$";
cin>>bonus;
cout<<"\n";
}
grossPay = employee.salary + bonus;
cout<<"\tThe gross pay for this employee is: $"<<grossPay<<endl;
}
system("pause");
return 0;
}
that fixed that but, I am getting a strange error. When I input easy numbers like $10 and hour and 10 hours it works fine and tells me gross pay is $100 but when I do $8.75 payRate and 19.5 hours the math goes wrong. It should say gross pay $170.63 but instead it says gross pay $166.25 Anyone got a clue why?