//
// Using the switch statement to implement a sequence of parallel alternatives
// Using a switch statement to select a range of scores
#include<iostream>
usingnamespace std;
int main()
{
int score;
cout << "Enter your test score: ";
cin >> score;
switch (score/10)
{ case'10':
break;
case'9': cout << "Your grade is an A." << endl;
break;
case'8': cout << "Your grade is an B." << endl;
break;
case'7': cout << "Your grade is an C." << endl;
break;
case'6': cout << "Your grade is a D." << endl;
break;
case'5':
break;
case'4':
break;
case'3':
break;
case'2':
break;
case'1':
break;
case'0' :
cout << "Your grade is an F." << endl;
break;
default: cout << "Error: score is out of range.\n";
system("pause");
return 0;
}
cout << "Good-bye." << endl;
}
My program does not return the grade, and it just shuts down after inputting a numerical score. Any ideas as to how to fix this?
Your switch statement checks for chars not ints. Just remove the single quotes (' ') from your numbers. You should also move the return statement to the end of the program.
//
// Using the switch statement to implement a sequence of parallel alternatives
// Using a switch statement to select a range of scores
#include<iostream>
usingnamespace std;
int main()
{
int score;
cout << "Enter your test score: ";
cin >> score;
switch (score/10)
{ case'10':
break;
case 9: cout << "Your grade is an A." << endl;
break;
case 8: cout << "Your grade is an B." << endl;
break;
case 7: cout << "Your grade is an C." << endl;
break;
case 6: cout << "Your grade is a D." << endl;
break;
case 5:
break;
case 4:
break;
case 3:
break;
case 2:
break;
case 1:
break;
case 0 :
cout << "Your grade is an F." << endl;
break;
default: cout << "Error: score is out of range.\n";
}
cout << "Good-bye." << endl;
system("PAUSE");
return 0;
}