Grading program not working

I have a program here for grading but it says that there are duplicate case values? Please help me fix my program.

[#include <iostream>

using namespace std;

int grading (int a,int b)
{
int result;
result = a / b;
return result;
}

int main()
{
int a, b, result;
cout << "Please type the number of questions you got correct: ";
cin >> a;
cout << endl;
cout << "Please type the total numbr of questions: ";
cin >> b;

result = grading (a, b);

switch (result)
{
case 0-59:
cout << "F" << endl; break;

case 60-69:
cout << "D" << endl; break;

case 70-79:
cout << "C" << endl; break;

case 80-89:
cout << "B" << endl; break;

case 90-99:
cout << "A" << endl; break;
case 100:
cout << "Perfect" << endl; break;
default:
cout << "Error" << endl; break;
}
return 0;
}
Code tags:
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
#include <iostream>

using namespace std;

int grading (int a,int b)
{
	int result;
	result = a / b;
	return result;
}

int main()
{
	int a, b, result;
	cout << "Please type the number of questions you got correct: ";
	cin >> a;
	cout << endl;
	cout << "Please type the total numbr of questions: ";
	cin >> b;

	result = grading (a, b);

	switch (result)
	{
	case 0-59:
		cout << "F" << endl; 
		break;
	case 60-69:
		cout << "D" << endl; 
		break;
	case 70-79:
		cout << "C" << endl; 
		break;
	case 80-89:
		cout << "B" << endl; 
		break;
	case 90-99:
		cout << "A" << endl; 
		break;
	case 100:
		cout << "Perfect" << endl; 
		break;
	default:
		cout << "Error" << endl; 
		break;
	}
	return 0;
}


Your problem is that when you do this:
case 60-69:
The compiler does a once-over and then compiles it as:
case -9:
Since you have multiple -9's there, you have your error.

Now this is actually a pretty invalid way to do it. What you want is an else-if statement like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  if (result < 60)
    cout << "F" << endl;
  else if (result < 70)
    cout << "D" << endl;
  else if (result < 80)
    cout << "C" << endl;
  else if (result < 90) 
    cout << "B" << endl;
  else if (result < 100)
    cout << "A" << endl;
  else if (result == 100)
    cout << "PERFECT!" << endl;
  else
    cout << "Error" << endl;


This is equivalent code to what you were trying to do except it doesn't check for <0 conditions. It'd be easy enough to include that.

The second problem with your code is your grading(int,int) function. You divide the number of correct answers by the number of questions. That'll give you a number between 0 and 1. The fact that both inputs are integers will cause integer division which will therefore result in either a 0 or a 1. Try something like this:
1
2
3
4
5
6
int grading (int a,int b)
{
int result;
result = a*100 / b;
return result;
}
Last edited on
thanks! I am starting to learn c++ and people like you help me out alot!
Topic archived. No new replies allowed.