I'm having a couple of problems w/ getting the program to end when the user enters 'Q' after they've been asked if they want to try again. Also, after enter a negative int is entered for employee id then either the program moves to displayEmployeeInfo & shows the info that's been already entered or if no info had been entered it should skip displayEmployeeInfo altogether and just display the total payroll at zero. Sorry if I'm not explaining this well but here are the samples:

Sample Run:

Enter employee ID ====> -5

Total Payroll for this month: $0.00

Sample Run #3:

Enter employee ID ====> 123
Enter payroll status ====> t
*** Invalid payroll status ***
Do you want to try again or quit?
Type Q or q to quit, any other key to continue: U
Re-enter: 's' or 'S' for salaried, 'h' or 'H' for hourly,
'c' or 'C' for commissioned ====> x

*** Invalid payroll status ***
Do you want to try again or quit?
Type Q or q to quit, any other key to continue: x
Re-enter: 's' or 'S' for salaried, 'h' or 'H' for hourly,
'c' or 'C' for commissioned ====> H
Enter number of hours worked this month ====> 160

Enter employee ID ====> -9

Employee ID Payroll Status Gross Pay

123 Hourly $3000


Total Payroll for this month: $3000.00

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

int getEmployeeData (int[], char[], int, float[]);
float calculateGrossPay(char[], float[], int, float[], string[], float*);
void displayEmployeeInfo (int[], string[], int, float[]);
bool tryAgain (char);
bool isValidStatus (char);
//bool isValidID (int);

int main()
{
	const int numEmp = 4;
	int empId[numEmp], size;
	char payrollStat[numEmp];	
	float gross[numEmp];
	float pay[numEmp];
	float totalPay = 0;
	string nameStatus[numEmp];

	size = numEmp;

	getEmployeeData(empId, payrollStat, size, gross);
	calculateGrossPay(payrollStat, gross, size, pay, nameStatus, &totalPay);
	cout << fixed << showpoint << setprecision(2);	
	displayEmployeeInfo (empId, nameStatus, size, pay);

	cout << "\nTotal Payroll for this month: " << "$" << totalPay << endl;

	system("pause");
	
	return 0;
}

int getEmployeeData (int e[], char ps[], int numEmp, float amt[])
{
	bool vStat;
	bool tAgain;
	char q;
	int x;

	for (x = 0; x < numEmp; x++)
	{
		cout << "Enter employee ID ====> ";
		cin >> e[x];

		if(e[x] < 0)
			return ;				// Not sure what I should be returning here
		else
		{
			cout << "Enter payroll status (s/h/c)====> ";
			cin >> ps[x];
			ps[x] = toupper(ps[x]);

			vStat = isValidStatus(ps[x]);
			if(vStat == false)
			{
				do
				{
					cout << "*** Invalid payroll status ***\n";
					cout << "Do you want to try again or quit?\n";
					cout << "Type Q or q to quit, any other key to continue: ";
					cin >> q;
					q = toupper(q);

					tAgain = tryAgain(q);
			
					if(tAgain == false)
					{
						cout << "   Re-enter: 's' or 'S' for salaried, 'h' or 'H' for hourly,\n";
						cout << "   'c' or 'C' for commissioned ====> ";
						cin >> ps[x];
						ps[x] = toupper(ps[x]);	
						vStat = isValidStatus(ps[x]);
					}

				}while (vStat == false && q != 'Q');
			}

			if(vStat == true)
			{
				if (ps[x] == 'S')
				{
					cout << "Enter monthly salary ====> ";
				}
				else if (ps[x] == 'H')
				{
					cout << "Enter number of hours worked this month ====> ";
				}
				else if (ps[x] == 'C')
				{
					cout << "Enter total sales for this month ====> ";
				}
		
				cin >> amt[x];

				cout << "\n";
			}
		}
	}
	return amt[x];
}

float calculateGrossPay (char pStat[], float pay[] , int numEmp, float tot[], string nStat[], float* totalP)
{
	int y;
	for (y = 0; y < numEmp; y++)
	{
		if (pStat[y] == 'S')
		{
			nStat[y] = "Salaried";
			tot[y] = pay[y];
			*totalP += tot[y];
		}
		if (pStat[y] == 'H')
		{
			nStat[y] = "Hourly";
			tot[y] = pay[y] * 18.75;
			*totalP += tot[y];
		}
		if (pStat[y] == 'C')
		{
			nStat[y] = "Commissioned";
			tot[y] = (pay[y] * 0.06) + 1000.00;
			*totalP += tot[y];
		}
	}
	return *totalP;
}

void displayEmployeeInfo (int id[], string payS[], int numEmp, float gPay[])
{
	cout << "Employee ID " << " Payroll Status " << " GrossPay\n";
	cout << "___________ " << " ______________ " << " ________\n";
	for (int z = 0; z < numEmp; z++)
	{
		cout << id[z] << "\t" << payS[z] << "\t" << "$" << gPay[z] << endl;
	}
}

bool isValidStatus (char stat)
{
	if(stat == 'S' || stat == 'H' || stat == 'C')
		return true;
	else
		return false;
}

bool tryAgain (char q)
{
	if(q == 'Q')
		return true;
	else 
		return false;
}

Any help woould be greatly appreciated

Every time you enter any input, do you press the ENTER key? That key does go into the input buffer, ready for the next input. You need to clear it out.

Yes, sorry for being a bit naive

Edited 5 Years Ago by boliviano3: n/a

This article has been dead for over six months. Start a new discussion instead.