Switch/Default statement help

Hi
I have a switch statement that chooses between three employees' information for the rest of the program.
The switch works, but I was wondering how to make it so the default case always sends the user back to the beginning of the switch? (As in you could make an invalid entry infinite times, and the default sends you back to make the choice again infinite times). Here's the code:

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
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])

{
                //declaring variables
                
                int empID;
                char payrollType;
                double hoursWorked;
                double payRate;
                double regularPay;
                double overtimePay;
                double grossPay;
                double stateTax;
                double federalTax;
                double totalTax;
                double unionDues;
                int unionCode;
                double netPay;    
                            
                
                cout << "**************************************\n";
                cout << "          Payroll Calculator          \n";
                cout << "**************************************\n\n";    
                
                cout << "Enter employee ID " << endl;
                cin >> empID;
                

                switch(empID)
                {
                  case 123: payrollType = 'H', hoursWorked = 25, payRate = 15,  unionCode = 1;
                  break;
                  case 456: payrollType = 'h', hoursWorked = 40, payRate = 20, unionCode = 2;
                  break;
                  case 789: payrollType = 'H', hoursWorked = 50, payRate = 25, unionCode = 3;
                  break;
                  default:
                  cout << "Invalid entry. Enter employee ID " << endl;
                  cin >> empID;
                  break;
                  }
Could you not just put the whole switch statement within a do while loop?
I'm a bit new to C++, we haven't learned loops yet. All we've learned are arithmetic, if/else, and switch. Is there a way to do it with those?
I tried the do-while on the http://www.cplusplus.com/doc/tutorial/control/ page... no dice.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  switch(empID)
                {
                  case 123: payrollType = 'H', hoursWorked = 25, payRate = 15, unionCode = 1;
                  break;
                  case 456: payrollType = 'h', hoursWorked = 40, payRate = 20, unionCode = 2;
                  break;
                  case 789: payrollType = 'H', hoursWorked = 50, payRate = 25, unionCode = 3;
                  break;
                  default:
                  do{
                  cout << "Invalid entry. Enter employee ID " << endl;
                  cin >> empID;
                  }
                  while (default:)
                  break;
                  }
Last edited on
So pretty much you want to just keep repeating the switch statement until either 123, 456 or 789 is entered?

You could probably do it like this (although I'm sure that there is a better way to do it):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 do{
 cout << "Enter employee ID " << endl;
                cin >> empID;
                
	switch(empID)
	{
		case 123: payrollType = 'H', hoursWorked = 25, payRate = 15,  unionCode = 1;
		repeat = false;
		break;
		case 456: payrollType = 'h', hoursWorked = 40, payRate = 20, unionCode = 2;
		repeat = false;
		break;
		case 789: payrollType = 'H', hoursWorked = 50, payRate = 25, unionCode = 3;
		repeat = false;
		break;
		default:
		cout << "Invalid Entry." << endl;
		repeat = true;
		break;
	}
}while(repeat == true);


Make sure to declare bool repeat = false; with the rest of your variables.
Last edited on
Hey meerkat,

Awesome, thanks for the help! I'll give it a whirl. This is my first graded project in C++, I hope it goes well. This is the last task before it's complete.
Last edited on
Topic archived. No new replies allowed.