If you type in employee ID, and then a character (other than s, h or c) including the enter key. You get a message that says, "Do you want to try again or quit?" "Type q or Q to quit any other key to continue." If you hit any other key than q or Q, nothing happens.
I am trying to get the program to go to the else part of the function when another key than q or Q is hit, including the enter key. But I am out of ideas.
I hope someone out there can help. Any suggestions will be much appreciated. Thank you!
Someone did refer me to a website with this problem but I was lost with the example given!

#include <iostream>
#include <iomanip>
using namespace std;
#include <cstdlib>
#include <string>

bool getEmployeeData (int*, char*, double*, double*, double*, double);
float calculateGrossPay (char, double, double, double);
void displayEmployeeInfo(int, char, double);
bool tryAgain(char*);
bool isValidStatus(char*);

int main()
{
int ID = 0;
double Salary = 0;
double hourwrk = 0;
double sales = 0;
double grossPay = 0;
char pyrllSt = ' ';
float totalPayroll = 0.0;
bool result = true;
while (result)
{
result = getEmployeeData (&ID, &pyrllSt, &Salary, &hourwrk, &sales, totalPayroll);
if (result)
{
grossPay = calculateGrossPay(pyrllSt, Salary, hourwrk, sales);
totalPayroll += grossPay;
displayEmployeeInfo (ID, pyrllSt, grossPay);
}
else
{
cout << "Total Payroll for this month: " << endl;
cout << fixed << showpoint << setprecision(2);
cout << "$" << totalPayroll << endl;
}
}
return (1);
}

bool getEmployeeData (int *ID, char *pyrllSt, double *Salary, double *hourwrk, double *sales, double totalPayroll)
{
cout << "Enter employee ID: " << endl;
cin >> *ID;

if (*ID > 0)
{
cout << "Enter payroll status: " << endl;
cin >> *pyrllSt;
if (isValidStatus(pyrllSt))
{
if (*pyrllSt == 's' || *pyrllSt == 'S')
{
cout << "Enter monthly salary" << endl;
cin >> *Salary;
}

if (*pyrllSt == 'h' || *pyrllSt == 'H')
{
cout << "Enter number of hours worked this month: " << endl;
cin >> *hourwrk;
}

if (*pyrllSt == 'c' || *pyrllSt == 'C')
{
cout << "Enter total sales for this month: " << endl;
cin >> *sales;
}
}//if isValid Status
return true;
}
else
{
return false;
}
}

bool isValidStatus (char *pyrllSt)
{
bool isGood = true;

while (isGood)
{

isGood = (*pyrllSt == 's' || *pyrllSt == 'S' || *pyrllSt =='h' || *pyrllSt == 'H' || *pyrllSt == 'c' || *pyrllSt =='C');

if (isGood)
{
return true;
}
else
{
isGood = tryAgain(pyrllSt);
}
}
return false;
}

bool tryAgain(char *pyrllSt)
{
char quit;

cout << "Type Q or q to quit, any other key to continue: " << endl;
cin >> quit;

if (quit == 'q' || quit == 'Q')
{
cout << "*** Program Terminated ***" << endl;
return false;
}
else
{
cout << "Re-enter: 's' or 'S' for salaried, 'h' or 'H' for hourly, " << endl;
cout << "'c' or 'C' for commissioned." << endl;
cin >> *pyrllSt;
return true;
}
}

float calculateGrossPay (char pyrllSt, double Salary, double hourwrk, double sales)
{
float grossPay;
if (pyrllSt == 's' || pyrllSt == 'S')
{
grossPay = Salary;
}

if (pyrllSt == 'h' || pyrllSt == 'H')
{
grossPay = hourwrk * 18.75;
}

if (pyrllSt == 'c' || pyrllSt == 'C')
{
grossPay = sales * 0.06 + 1000.00;
}
return grossPay;

}

void displayEmployeeInfo (int ID, char pyrllSt, double grossPay)
{
cout << "Employee ID: " << ID << endl;
cout << "Payroll Status: " << pyrllSt << endl;
cout << "Gross Pay: " << fixed << showpoint << setprecision(2);
cout << "$" << grossPay << endl;
}

Greetings,

Looking throughout your code, you leave it somewhat impossible to allow a tryAgain() function work. As, lets say, getEmployeeData() takes 6 parameters; all of which are local variables in main(). If we were to literally try again we would have to send all 6 parameters again including our updated pyrllSt. Though in fact, tryAgain only gets and modifies that one variable only, so how would it be possible to call on getEmployeeData() again?

It probably isn't unless you make all of your variables global, or re-write the code more portably.


- Stack Overflow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.