Anyone know what to add to this code? Switch statements

I have an assignment where I am suppose to use switch to determine the letter grades. I don't know how to use it, so even when the average is 80, it still goes to default

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
  ude <iostream>
#include <string>
using namespace std;

int main()

{
	string name1, name2;
	int gr1;
	int gr2;
	int gr3;
	int gr4;
	int gr5;
	int avg;



	cout << "Enter the first and last name of the student. \n" << endl;
	cin >> name1 >> name2;
	cout << "\nStudent:" << name1 << " " << name2 << endl;
	
	cout << "\nEnter FIVE grades of STUDENT LAST NAME: " << name2 << endl;
	cout << "\n1. \n\n";
	cin >> gr1;
	cout << "\n2.  \n\n";
	cin >> gr2;
	cout << "\n3. \n\n";
	cin >> gr3;
	cout << "\n4. \n\n";
	cin >> gr4;
	cout << "\n5.\n\n";
	cin >> gr5;
	cout << "\n\n\n";
	
	avg = (gr1 + gr2 + gr3 + gr4 + gr5) / 5;
	cout << "Average of student: " << name1 << " " << name2<< " is " << avg;

	switch (avg)
	{
	case '80': cout << " Grade is B";
		break;
	default: 
		cout << "\n Grade is invalid ";
		
	}


	cout << "\n" << endl;

	return 0;

}
Last edited on
I may have hit a break, should I put the line of code for switch as :

switch (avg / 10)?
Yes, but you don't want to use single quotes in the case labels - that would make them characters instead of numbers.
Oh wow thanks, but how come when I enter the five grades which average into an 80, it still comes out to the default statement?
What is your current code? Remember you divided by 10, so your case statement for 80 should be case 8:
Oh my, well I'm still tampering with the one up above to see what can work so I only changed the switch to
<switch (avg / 100)>

, but if you mean that and I put it as out of 100 would I have to then make 99 cases?


Or maybe use some combination of greater than less than operators?
Last edited on
Got it, I just left the switch as what I put before
switch (avg)

and added
char num;

Still, I'm wondering if I have to write a case for every numerical grade 1-100
Last edited on
If you have 5 grades, and divide by anything other than 5, would you have a true average ?
I think you could use another variable (say final_grade) and possibly do something with avg that way.

The first thing that jumps out at me is you are using type int. so if I have a grade average of 75 and you need to reduce it, say /2 then that's 37.5, but with int, it'd be 37... You just screwed me out of 1/2 grade point... That being said... I'm going to keep reading this thread to find out the answer...

switch(avg / 10) is the correct thing to do in this case. Don't try random other things without thinking them through first.
Correct me if I'm wrong, would using switch(avg / 10) and having the average 79 using case 7 as giving a C round it off to so? I'm just a bit or so confused.

Sorry I wasn't sure what you were meaning pearlyman, but I think I do now. I'm not sure I want to try to make the code too complex. Maybe I should use be using double for what you said, I'd like to get more in-depth into what you said.

For the code itself, I think last week our professor was laughing at what our other professor was having us do, saying we would know very much afterwards how to use copy/paste

Well it's very handy, I know now for sure, and I think he was looking at us writing the assignment 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
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
137
138
139
140
#include <iostream>
#include <string>
using namespace std;

int main()

{
	string name1, name2;
	int gr1;
	int gr2;
	int gr3;
	int gr4;
	int gr5;
	int avg;
	char num; // Declared in order for switch statements to use numerals 



	cout << "Enter the first and last name of the student. \n" << endl;
	cin >> name1 >> name2;
	cout << "\nStudent:" << name1 << " " << name2 << endl;

	cout << "\nEnter FIVE grades of STUDENT LAST NAME: " << name2 << endl;
	cout << "\n1. \n\n";
	cin >> gr1;
	cout << "\n2.  \n\n";
	cin >> gr2;
	cout << "\n3. \n\n";
	cin >> gr3;
	cout << "\n4. \n\n";
	cin >> gr4;
	cout << "\n5.\n\n";
	cin >> gr5;
	cout << "\n\n\n";

	avg = (gr1 + gr2 + gr3 + gr4 + gr5) / 5;
	cout << "Average of student: " << name1 << " " << name2 << " is " << avg;


	switch (avg)

	{
	case 100: cout << " Grade is A";
		break;
	case 99: cout << " Grade is A";
		break;
	case 98: cout << " Grade is A";
		break;
	case 97: cout << " Grade is A";
		break;
	case 96: cout << " Grade is A";
		break;
	case 95: cout << " Grade is A";
		break;
	case 94: cout << " Grade is A";
		break;
	case 93: cout << " Grade is A-";
		break;
	case 92: cout << " Grade is A-";
		break;
	case 91: cout << " Grade is A-";
		break;
	case 90: cout << " Grade is A-";
		break;
	case 89: cout << " Grade is B+";
		break;
	case 88: cout << " Grade is B+";
		break;
	case 87: cout << " Grade is B+";
		break;
	case 86: cout << " Grade is B";
		break;
	case 85: cout << " Grade is B";
		break;
	case 84: cout << " Grade is B";
		break;
	case 83: cout << " Grade is B";
		break;
	case 82: cout << " Grade is B";
		break;
	case 81: cout << " Grade is B-";
		break;
	case 80: cout << " Grade is B-";
		break;
	case 79: cout << " Grade is C+";
		break;
	case 78: cout << " Grade is C+";
		break;
	case 77: cout << " Grade is C+";
		break;
	case 76: cout << " Grade is C";
		break;
	case 75: cout << " Grade is C";
		break;
	case 74: cout << " Grade is C";
		break;
	case 73: cout << " Grade is C";
		break;
	case 72: cout << " Grade is C-";
		break;
	case 71: cout << " Grade is C-";
		break;
	case 70: cout << " Grade is C-";
		break;
	case 69: cout << " Grade is D+";
		break;
	case 68: cout << " Grade is D+";
		break;
	case 67: cout << " Grade is D+";
		break;
	case 66: cout << " Grade is D";
		break;
	case 65: cout << " Grade is D";
		break;
	case 64: cout << " Grade is D";
		break;
	case 63: cout << " Grade is D";
		break;
	case 62: cout << " Grade is D-";
		break;
	case 61: cout << " Grade is D-";
		break;
	case 60: cout << " Grade is D+-";
		break;
	case 59: cout << " Grade is F";
		break;
	


	default:
		cout << "\n Grade is invalid ";

	}


	cout << "\n" << endl;

	return 0;

}


I'm going to see how to implement what LB said, it should be a simple couple of lines if I'm imagining his solution correctly although I'm not sure how giving specific letter grades is going to work with it. I wonder how decimaled grades is going to work.
Integer division always rounds down, so 79/10 is 7.
Oh okay thanks a lot LB, this was a very rugged solution I know. It wouldn't be right to leave the default for simple grading programs like this as an F right?
IMO switch statements should not be used in this case, but your assignment requires it so I will not argue. I only ask that you consider what would happen if a teacher gave extra credit and a student got a 110.
Topic archived. No new replies allowed.