Switch statement in C++
Consider:
#include <iostream>
using namespace std;
int main()
{
int score;
char grade;
cout << "Enter your score: " << endl;
cin >> score;
if (score >= 90)
grade = 'a';
if (score >= 80)
grade = 开发者_如何转开发'b';
if (score >= 70)
grade = 'c';
if (score >= 60)
grade = 'd';
else
grade = 'f';
cout << grade << endl;
switch (grade) {
case 'a':
cout << "Good job" << endl;
break;
case 'c':
cout << "Fair job" << endl;
break;
case 'f':
cout << "Failure" << endl;
break;
default:
cout << "invalid" << endl;
}
cin.get();
return 0;
}
Why is it giving my default switch case when I enter 95
when I should be getting case 'a'?
You're missing a bunch of else
s, or doing the comparisons in the wrong order.
95 is greater than 90, but it's also greater than 80, 70 and 60. So you'll get a 'd'.
(And you're not handling 'd' in your switch.)
I believe you want
if (score >= 90)
grade = 'a';
else if (score >= 80)
grade = 'b';
else if (score >= 70)
grade = 'c';
else if (score >= 60)
grade = 'd';
else
grade = 'f';
What you have does not mutually exclude any but the last two cases, 60 and above and lower. Your code doesn't short circuit; it checks all of 1 through 5.
if (score >= 90) // 1.
grade = 'a';
if (score >= 80) // 2.
grade = 'b';
if (score >= 70) // 4.
grade = 'c';
if (score >= 60) // 5.
grade = 'd';
else
grade = 'f';
I think you want to use 'else if'. It is falling down to the last if "score >= 60" which is true, and grade then equals "d", which produces the default case in your switch statement.
You have specified it such that your 95 satisfies all the cases: 95 is bigger than 90, but also bigger than 80, than 70, etc.
In this case, the last one wins.
You can solve it by either using else
s, or by wrapping it in a function and returning as soon as you know the grade you need:
char grade(int score) {
if (score >= 90) return 'a';
if (score >= 80) return 'b';
...
}
Because all score
comparisons are not combined with if/else if
conditions. They are independent if
statements. Thus grade
gets overwritten for 95
.
The if branches are ordered wrong (or you need to provide else branches like so:)).
See it live here: http://ideone.com/2uSZT
#include <iostream>
using namespace std;
int main()
{
int score;
char grade;
cout << "Enter your score:" << endl;
cin >> score;
if (score >= 90)
grade = 'a';
else if (score >= 80)
grade = 'b';
else if (score >= 70)
grade = 'c';
else if (score >= 60)
grade = 'd';
else
grade = 'f';
cout << grade << endl;
switch (grade)
{
case 'a':
cout << "Good job" << endl;
break;
case 'c':
cout << "Fair job" << endl;
break;
case 'f':
cout << "Failure" << endl;
break;
default:
cout << "invalid" << endl;
}
cin.get();
return 0;
}
It's because of your if statements up top. You should be using else ifs instead of individual ifs. Your if for 90 is following through, and then so are all the others. Your letter "a" is essentially being overwritten because 95 is >= to all of the other conditions. Using an else if will break the rest of the checks when a true one is found.
if (score >= 90)
grade = 'a';
else if (score >= 80)
grade = 'b';
else if (score >= 70)
grade = 'c';
else if (score >= 60)
grade = 'd';
else
grade = 'f';
You need to improve your if conditions.
You are checking score >= no
. The input 95
executes all the if statements and the last executed statement was the d
. Now in your switch statement, case d
is not present so it executes the default
one.
You've gotten some answers already, but I think I'll suggest a slightly different possibility that gets rid of most of the control flow and substitutes a bit of math:
char grades[] = "00000012344";
char *messages[] = {
"Excellent Job",
"Good job",
"Average job",
"Mediocre Job",
"Failure"
};
if (score < 0 || score > 100)
std::cout << "Invalid score";
else {
int grade = grades[score/10];
std::cout << messages[grade];
}
So, we use score/10
to turn scores of 0-100 to 0-10. We then look up the appropriate grade for a score, with f=0, d=1, c=2, b=3 and a=4. We use that to select and print out the appropriate message. I've added messages (that may or may not be quite what you want) for the letters you skipped.
精彩评论