I am trying to have this program prompt the user if there is another employee that will need to input his info for the same output questions. So instead of ending with press any key to continue..., I want it to ask if there is another employee and if the answer is yes, the program repeats the output questions immediately without displaying anything until the user puts no, that there is no more employees and so then it finally displays all the output data from all the employees.
So basically, How do I get the program to repeat itself and save the inputted info until the end?
#include <iostream>
usingnamespace std;
int main () // Initiates the main sequence to allow for coding
{
int empID; // Data in which user will input info that will be have arithmetic done on it and will be displayed
cout << "empID ";
cin >> empID;
while (empID < 100 || empID > 800) // Loop that sets this statement on repeat until integer is in proper set of values
{
cout << "Invalid Input, must be a value between 100 - 800\n";
cout << "empID ";
cin >> empID;
}
char payrollType;
char H = 'H';
char h = 'h';
cout << "payrollType ";
cin >> payrollType;
while (payrollType != H && payrollType != h) // Loop that sets this statement on repeat until integer is in proper set of values
{
cout << "Invalid Input, must be either H or h\n";
cout << "payrollType ";
cin >> payrollType;
}
double hoursWorked;
cout << "hoursWorked ";
cin >> hoursWorked;
while (hoursWorked < 0 || hoursWorked > 60) // Loop that sets this statement on repeat until integer is in proper set of values
{
cout << "Invalid Input, must be a value between 0 - 60\n";
cout << "hoursWorked ";
cin >> hoursWorked;
}
double payRate;
cout << "payRate ";
cin >> payRate ;
while (payRate < 8.50 || payRate > 45.00) // Loop that sets this statement on repeat until integer is in proper set of values
{
cout << "Invalid Input, must be a value between 8.50 - 45.00\n";
cout << "payRate ";
cin >> payRate;
}
int unionCode;
cout << "unionCode ";
cin >> unionCode;
while (unionCode != 1 && unionCode != 2 && unionCode !=3) // Loop that sets this statement on repeat until integer is in proper set of values
{
cout << "Invalid Input, must be a value between 1 - 3\n";
cout << "unionCode ";
cin >> unionCode;
}
// This next section consists of the code that outputs the data.
char employeePayInformation;
cout << "Employee Pay Information: \n";
int EmployeeId;
cout << "Employee Id: " << empID << endl;
char PayrollType;
cout << "Payroll Type: " <<payrollType << endl;
int UnionCode;
cout << "Union Code: " << unionCode << endl;
int HoursWorked;
cout << "Hours Worked: " << hoursWorked << endl;
int PayRate;
cout << "Pay Rate: " << payRate << endl;
double RegularPay = hoursWorked*payRate; //Arithmetic instilled to make sure regularPay gets done correctly
cout << "Regular Pay: " << RegularPay << endl;
double OvertimePay;
if (hoursWorked < 40) //If and else statements that make sure the variable gets worked over do to the dependant clause
OvertimePay = 0;
else
OvertimePay = PayRate * 1.5 * (hoursWorked - 40);
cout << "Overtime Pay: " << OvertimePay << endl;
double GrossPay = RegularPay + OvertimePay;
cout << "Gross Pay: " << GrossPay << endl;
double StateTax;
if ( (GrossPay >= 500) && (GrossPay <= 1000) )
StateTax = GrossPay * .03;
else
StateTax = 0;
if (GrossPay > 1000) // If gross pay is > than 1000, 5% is deduced
StateTax = GrossPay * .05;
else
StateTax = 0;
cout << "State Tax: " << StateTax << endl;
double FederalTax;
if ((GrossPay >= 500) && (GrossPay <= 1000)) // Federal Tax arithmetic
FederalTax = GrossPay * .05;
else
FederalTax = 0;
if (GrossPay > 1000)
FederalTax = GrossPay * .07;
else
FederalTax = 0;
cout << "Federal Tax: " << FederalTax << endl;
double TotalTax = StateTax + FederalTax;
cout << "Total Tax " << TotalTax << endl;
double UnionDues;
if (unionCode = 1)
UnionDues = 15;
else
UnionDues = 35;
if (unionCode = 2)
UnionDues = 25;
else
UnionDues = 35;
cout << "Union Dues: " << UnionDues << endl;
double NetPay = GrossPay - (StateTax + FederalTax + UnionDues);
cout << "Net Pay: " << NetPay << endl;
cout << "Press any key to continue . . . ";
cin.get(); // To display everything that needs displaying
cin.get();
return 0; // Marks the end of the coding
}
I just thought of it now, I could use a do while function to repeat output questions but how do I set it to maximum of 5 employees, so that the do while will stop looping until it either hits 5 employees or the user puts no, when it promps if there is another employee
You can use a for loop, or make a counter for your while loop
1 2 3 4 5
for (int temp = (lower bound goes here, typically 0) ; temp < (max number - 1 goes here if you just use less than, use less than or equal to include this number); temp++){
//do this maxnumber - lowerbound - 1 times.
}
or
1 2 3 4 5 6 7
int counter = 1;
while (counter <= highest number){
//do stuff here
counter++;
}
For starters you need more data storage. To do this with minimal change to your methods use arrays, but declare everything at the start of main (so all data is visible throughout main).
int main(void)
{
int empID[5];
char payrollType[5];
double hoursWorked[5];
// and so on for every Employee variable
char repeat = 'n';
int N = 0;// number of employees entered
// enter loop to prompt for each employee
do
{
repeat = 'n';// presume no repeat
cout << "empID ";
cin >> empID[N];
while (empID[N] < 100 || empID[N] > 800) // Loop that sets this statement on repeat until integer is in proper set of values
{
cout << "Invalid Input, must be a value between 100 - 800\n";
cout << "empID ";
cin >> empID[N];
}
// and so on for each value to collect except using [N] with each
// after all data has been collected for this employee
N++;// increment the index
if( N < 5 )// permit another employee data set
{
cout << endl << "Another Employee (y/n)? " << std::flush;
cin >> repeat;
}
}while( repeat=='y' && N<5);// loop is exited if user enters 'n' or if N == 5
// now handle any output.
return 0;
}