Switch statement has no effect?

As far as I know, I think the rest of my code are fine except for the switch statement part. Whenever I run the program, it always says that the switch statement has no effect.

In this lab, write a C++ program that calculates an employee’s end-of-year bonus and prints the employee’s name, yearly salary, performance rating, and bonus. In this program, bonuses are calculated based on employees’ annual salary and their performance rating.

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
 #include <iostream>
#include <string>
using namespace std;
int main()
{
   string employeeFirstName; 
   string employeeLastName; 
   double employeeSalary;	
   int employeeRating; 
   int employeeBonus;
   const double BONUS_1 = .25;
   const double BONUS_2 = .15;
   const double BONUS_3 = .10; 
   const double NO_BONUS = 0.00;
   const int RATING_1 = 1;
   const int RATING_2 = 2;
   const int RATING_3 = 3; 
       	
   cout << "Enter employee's first name: ";
   cin >> employeeFirstName;
   cout << "Enter employee's last name: ";
   cin >> employeeLastName;
   cout << "Enter employee's yearly salary: ";
   cin >> employeeSalary;
   cout << "Enter employee's performance rating: ";
   cin >> employeeRating; 
 
    switch(employeeBonus)
 {        
    case RATING_1 : employeeSalary * BONUS_1;
    break;
    case RATING_2 : employeeSalary * BONUS_2;
    break;
    case RATING_3 :  employeeSalary * BONUS_3;
    break;
    default : cout << NO_BONUS << endl; 
    break;
}
   
   cout << "Employee Name: " << employeeFirstName << " " << employeeLastName << endl;
   cout << "Employee Salary: $" << employeeSalary << endl;
   cout << "Employee Rating: " << employeeRating << endl;
   cout << "Employee Bonus: $" << employeeBonus << endl;
   return 0;
} 	


Testing:
Employee Name: Jeanne Hanson
Employee Salary: $70000
Employee Rating: 2
Employee Bonus: $10500 =====this part is where the output I get is wrong...instead of showing $10500 which is the correct value...it shows $32767.

Please, is there a way to fix this?

Yes, there is a way to fix it.
employeBonus is undefined
int employeeBonus; is never assigned any value. You run your current program 10 times, you will likely get 10 different output as employeeBonus

Fix like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//...
int employeeBonus = 0; // good idea to always initialize your variables
//...

 switch(employeeRating)
 {        
    case RATING_1 : 
    employeeBonus = employeeSalary * BONUS_1; // <-- the logic that you missed 
    break;
    case RATING_2 : 
      employeeBonus = employeeSalary * BONUS_2;
    break;
    case RATING_3 :  
      employeeBonus = employeeSalary * BONUS_3;
    break;
    default : cout << NO_BONUS << endl; 
    break;
} 

//ouput results

   return 0;
} 	

Last edited on
I did what you said but the output for the employeeBonus always seems to be zero?

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
  #include <iostream>
#include <string>
using namespace std;
int main()
{
   string employeeFirstName; 
   string employeeLastName; 
   double employeeSalary;	
   int employeeRating; 
   int employeeBonus = 0;              //I initialize the variable to zero
   const double BONUS_1 = .25;
   const double BONUS_2 = .15;
   const double BONUS_3 = .10; 
   const double NO_BONUS = 0.00;
   const int RATING_1 = 1;
   const int RATING_2 = 2;
   const int RATING_3 = 3;    
		
   cout << "Enter employee's first name: ";
   cin >> employeeFirstName;
   cout << "Enter employee's last name: ";
   cin >> employeeLastName;
   cout << "Enter employee's yearly salary: ";
   cin >> employeeSalary;                                         
   cout << "Enter employee's performance rating: ";
   cin >> employeeRating; 

    switch(employeeBonus)
 {        
    case RATING_1 : employeeBonus = employeeSalary * BONUS_1;
    break;
    case RATING_2 : employeeBonus = employeeSalary * BONUS_2;
    break;
    case RATING_3 :  employeeBonus = employeeSalary * BONUS_3;
    break;
    default : cout << NO_BONUS << endl; 
    break;
}
   cout << "Employee Name: " << employeeFirstName << " " << employeeLastName << endl;
   cout << "Employee Salary: $" << employeeSalary << endl;
   cout << "Employee Rating: " << employeeRating << endl;
   cout << "Employee Bonus: $" << employeeBonus << endl;  //However, the output for the employeeBonus is always zero?
   return 0;
} 	


Last edited on
It is because you passed the wrong value in the switch statement. It does nothing since there is no matching case. You are passing employeeBonus instead of employeeRating

Compare line 5 of my code with line 28 of your recent code
Last edited on
Sorry, I missed that...Thank you so much T^T
it works perfectly well now.
Last edited on
Topic archived. No new replies allowed.