How to repeat Output variables?

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?

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
using namespace 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 
}


Last edited on
closed account (LTXN8vqX)
here is the code snippet for the option of yes or no u wanted:
1
2
3
4
5
6
7
8
9
10
char option;
switch(option) 
{
case 'y':
Put what u want it to do.
break;
case 'n':
put what you want it to do
break;
}


Hope it helps
appreciate it bro

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++;

}



edit: hehe 512 posts... in binary 0010 0000 0000
Last edited on
closed account (D80DSL3A)
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).

Here's an outline:
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
30
31
32
33
34
35
36
37
38
39
40
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;	
}
Topic archived. No new replies allowed.