Need Help

Writing a program and having some issues. Errors are not received, but if a number other than 1-4 is entered into the insurance information it does not produce an error and instead loops back to entering the employee hours. If i re-enter the employee hours it then goes back to the 1-4 option but then goes right back to employee hours even if you enter a valid option. Also if within the code you enter 0 to exit...it will not work, only at the very beginning. Can someone help me please and tell me what you think may be wrong?

Thanks,
Cennet

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

// ***************************** PROTOTYPES ********************************
double bonus(double empGrossPay);

double insur(int insStat, double empTotalPay);

double taxes(double empGrossPay);

int main(){

// Declare Variables
int empID;
char empPos;
double hrsWorked;
int insStat;
double empGrossPay;
double empTotalPay;
double medical = 100;
double empSoc;
double Deduc;
double empNetPay;

// Ask user for employee ID number, enter 0 to exit

cout<<"Enter the employee ID number. ";
cout<<"Enter a 0 to quit: ";
cin>>empID;
// System clear to keep it neat
system("cls");
// While loop to look for 0
while (empID > 0){
// Enter employee position

cout<<"Enter the position. "<<endl;
cout<<"Enter 'm' for Manager,"<<endl;
cout<<"Enter 's' for Supervisor, or "<<endl;
cout<<"Enter 'e' for Employee:"<<endl;
cin>>empPos;


// System clear to keep it neat
system("cls");
// While statement to validate employee position
while (empPos == 'm' || empPos == 's' || empPos == 'e'){

cout<<"Enter the hours worked. It must be greater than 0: ";
cin>>hrsWorked;
// System clear to keep it neat
system("cls");
// If statement to validate hours worked
if (hrsWorked > 0){
cout<<"What is the Employee Insurance Type: "<<endl;
cout<<"Enter '1' for single:"<<endl;
cout<<"Enter '2' for single with children:"<<endl;
cout<<"Enter '3' for married:"<<endl;
cout<<"Enter '4' for married with children:"<<endl;
cin>>insStat;
// System clear to keep it neat
system("cls");
// While statement to validate insurance status
while (insStat == '1' || insStat == '2' || insStat == '3' || insStat == '4'){

// Calculate employee pay rate
double payRate;

switch (empPos)
{
case 'm': payRate = 700.00;
break;
case 's': payRate = 600.00;
break;
case 'e': payRate = 500.00;
break;
default: payRate = 0.00;
}
// Final calculations

empGrossPay = hrsWorked * payRate;
empTotalPay = bonus(empGrossPay) + empGrossPay;
empSoc = empGrossPay * .05;
Deduc = taxes(empGrossPay) + empSoc + medical + insur(insStat, empGrossPay);
empNetPay = empTotalPay - Deduc;

// Display layout

cout<<"--------"<<"EMPLOYEE INPUT DATA"<<"--------"<<endl;
cout<<endl;
cout<<left;
cout<<"Employee Number: "<<right<<setw(25)<<empID<<endl;
cout<<left;
cout<<"Employee Position: "<<right<<setw(20)<<empPos<<endl;
cout<<left;
cout<<"Employee hours worked: "<<right<<setw(20)<<hrsWorked<<endl;
cout<<left;
cout<<"Employee Insurance Type: "<<right<<setw(14)<<insStat<<endl;
cout<<endl;
cout<<"-----------------------------------------------------------"<<endl;
cout<<endl;
cout<<left<<"Employee Gross Pay: "<<right<<setw(15)<<fixed<<setprecision(2)<<empGrossPay<<endl;
cout<<left<<"Employee Bonus Pay: "<<right<<setw(15)<<fixed<<setprecision(2)<<bonus(empGrossPay)<<endl;
cout<<left<<"Employee Total Pay: "<<right<<setw(23)<<fixed<<setprecision(2)<<empTotalPay<<endl;
cout<<endl;
cout<<left<<"Employee Ins. Amount: "<<right<<setw(13)<<fixed<<setprecision(2)<<insur(insStat, empGrossPay)<<endl;
cout<<left<<"Employee Tax Amount: "<<right<<setw(14)<<fixed<<setprecision(2)<<taxes(empGrossPay)<<endl;
cout<<left<<"Employee S.S. Amt: "<<right<<setw(16)<<fixed<<setprecision(2)<<empSoc<<endl;
cout<<left<<"Employee Medical Cost: "<<right<<setw(12)<<fixed<<setprecision(2)<<medical<<endl;
cout<<left<<"Employee Total Ded.: "<<right<<setw(22)<<fixed<<setprecision(2)<<Deduc<<endl;
cout<<endl;
cout<<left<<"Employee Net Pay: "<<right<<setw(25)<<fixed<<setprecision(2)<<empNetPay<<endl;

system("pause");
system("cls");


//Restart or press 0 to exit

cout<<"Enter the employee ID number. ";
cout<<"Enter a 0 to quit: "<<endl;
cin>>empID;
system("cls");



}
}
else cout<<"Your hours worked must be > 0! Please enter a valid value: "<<endl;


}

}
system("pause");
return 0;
}

//*************************** FUNCTIONS ************************************
// Bonus

double bonusPay1 = 1000;
double bonusPay2 = 600;
double bonusPay3 = 0;

double bonus(double empGrossPay)

{
if (empGrossPay >= 12000)
return bonusPay1;
else if (empGrossPay >= 9000)
return bonusPay2;
else
return bonusPay3;
}



// Calculate insurance rate based on pay
double insur(int insStat, double empGrossPay)
{
if (insStat = 1)
return empGrossPay * .02;
else if (insStat = 2)
return empGrossPay * .03;
else if (insStat = 3)
return empGrossPay * .04;
else if (insStat = 4)
return empGrossPay * .05;
else
return empGrossPay * 0;
}


// Determine taxes
double taxes(double empGrossPay)
{
if (empGrossPay >= 7000)
return empGrossPay * .15;
else if (empGrossPay >= 4000)
return empGrossPay * .10;
else if (empGrossPay >= 2000)
return empGrossPay * .05;
else
return empGrossPay * 0;
}
when you declared the insStat variable you specified it to be an integer.

int insStat;

then you get the insStat value from the user, so insStat will either have the value of 1, 2, 3 or 4.

cin>>insStat;


then on your loop condition you have

while (insStat == '1' || insStat == '2' || insStat == '3' || insStat == '4')

you're suddenly comparing insStat to characters, the characters '1', '2', '3', and '4' will be converted to their ascii values, and they will never be 1, 2, 3, or 4.

Last edited on
Ok so that worked, thanks so much. But its still looping back to the employee hours worked if we enter the wrong insurance number. Any ideas?
I'm having some difficulty matching up the brackets so it's taking me a bit longer to debug lolz. Give me a few and I'll try to see what I can do. :P
K thanks so much
well it's because of the condition

while (empPos == 'm' || empPos == 's' || empPos == 'e'){

when you enter an invalid insurance number, the empPos is still going to be either 'm', 's', or 'e', and the condition will be true, so it will loop again. What you'll have to do is to reset it to an invalid value like:

empPos = '\0';

in case an invalid value was entered, or when you get to the end of the loop, so it will stop looping. If that makes sense.
Last edited on
where would that need to go? and does that have to do with it looping back to the employee hours?
the general form of a while loop is

1
2
3
4
while(condition is true)
{
    // do something
}



so once you've entered a valid empPos value, your loop condition will always be true on this line.

while (empPos == 'm' || empPos == 's' || empPos == 'e'){

Therefore it will not stop looping, and you end up with an infinite loop. I'm not really sure why you'd really do that though. Most of the time you will only try to keep looping if an invalid value was entered, so you can give the user a chance to enter a valid value. So the loop condition doesn't really make sense, maybe what you really intended was something like.

1
2
3
4
5
6
7
8
9
10
// prompt user to enter a value like "enter employee position>  "
cin >> empPos; // get user input

// check if entered value was invalid
while(empPos != 'm' || empPos != 's' || empPos != 'e')
{
    // invalid value was entered
    // prompt user with an error message like "invalid input, please try again..."
    cin >> empPos; // get user input again
}


Then when empPos becomes valid it will bypass the loop and continue to execute the next statements.
Last edited on
The directions i have said that the main function should also contain a loop that validates the employee position entered by the user. :(
plus that's for the employee position, which isn't where we're really having an issue, it's within the insurance code part for the 1,2,3,4.
actually, that is what's causing the issue. Your insurance code is within that loop, and that's why it's going back to that point.

But its still looping back to the employee hours worked if we enter the wrong insurance number.


whether you enter the wrong insurance number, or a correct insurance number. empPos will always remain valid, hence the condition of the loop will never become false making it loop over and over again and returning to the hours worked part.

what you actually need to do is something like this instead

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
cout<<"Enter the position. "<<endl;
cout<<"Enter 'm' for Manager,"<<endl;
cout<<"Enter 's' for Supervisor, or "<<endl;
cout<<"Enter 'e' for Employee:"<<endl;
cin>>empPos;


// System clear to keep it neat
system("cls");
// While statement to validate employee position
while (empPos != 'm' || empPos != 's' || empPos != 'e'){
    cout<<"Enter the position. "<<endl;
    cout<<"Enter 'm' for Manager,"<<endl;
    cout<<"Enter 's' for Supervisor, or "<<endl;
    cout<<"Enter 'e' for Employee:"<<endl;
    cin>>empPos;

    // System clear to keep it neat
    system("cls");
} // end of while loop

cout<<"Enter the hours worked. It must be greater than 0: ";
cin>>hrsWorked;
// System clear to keep it neat
system("cls");
.
.
.
// and so on 
Last edited on
so should i not have any of the loops nested, just one after the other? also when all correct information was entered it ran fine, it's just that it wasn't validating one way or the other when it came to entering the 1,2,3, or 4 for the tax information. now it won't go past the enter hours part.
you need to have loops nested within the first main loop, but for the loops that validate the user input, you need them to loop only the code that enters new values in the variables they're validating. and not the rest. it's more like the following

1
2
3
4
5
6
7
8
// get user input
while(input != valid value) // while the user input isn't a valid value
{
    // get user input again
}

// other statements go here outside of the loop, so they don't get executed again
// until the main loop actually loop all over again. 

Last edited on
Topic archived. No new replies allowed.