#include <iostream>
usingnamespace std;
int main()
{
char a;
int b;
cout << "Please input letter grade: ";
cin >> a;
if (a == 'A'||a == 'a'){
b = 1;
}
if (a == 'B'||a == 'b'){
b = 2;
}
if (a == 'C'||a == 'c'){
b = 3;
}
if (a == 'D'||a == 'd'){
b = 4;
}
if (a == 'F'||a == 'f'){
b = 5;
}
else
b = 6;
switch (b)
{
case 1:
cout << "Your grade is a 95." << endl;
break;
case 2:
cout << "Your grade is an 85." << endl;
break;
case 3:
cout << "Your grade is a 75." << endl;
break;
case 4:
cout << "Your grade is a 65." << endl;
break;
case 5:
cout << "Your grade is a 50." << endl;
break;
case 6:
cout << "Error!" << endl;
break;
}
return 0;
}
switch (b)
{
case 1:
cout << "Your grade is a 95." << endl;
break;
case 2:
cout << "Your grade is an 85." << endl;
break;
case 3:
cout << "Your grade is a 75." << endl;
break;
case 4:
cout << "Your grade is a 65." << endl;
break;
case 5:
cout << "Your grade is a 50." << endl;
break;
default:
cout << "Error!" << endl;
break;
}
in your switch/case, default will happen when none of the other cases happen giving you the correct result.
The alternative option is to replace each of your if... if... statements with if ... else if... else if.... You can also replace your switch with putting the statements in the actual if blocks, removing the need for a variable:
1 2 3 4 5 6 7 8
if (a == 'A' || a == 'a') {
cout << "Your grade is a 95.\n";
} elseif (a == 'B' || a == 'b') {
cout << "Your grade is an 85.\n";
// repeat the above two lines for C, D, F
} else {
cout << "Error!\n";
}