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;
// 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
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.
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.
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.
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
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
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.