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;
#include <iostream>
usingnamespace 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:
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;
}