问题
#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 me my default switch case when i enter 95
when i should be getting case a
回答1:
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.)
回答2:
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';
回答3:
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.
回答4:
You have specified it such that your 95 satisfies all the cases: 95 is bigger than 90, but also bigger than 80 and 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';
...
}
回答5:
It's because of your if statements up top. you should be using else ifs instead of individual ifs. Whats happening is 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 coniditons. 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';
回答6:
Because all score
comparisons are not combined with if/else if
conditions. They are independent if
statements. Thus grade
gets overwritten for 95
.
回答7:
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;
}
回答8:
you need to improve your if conditions, you are checking score >= no.
the input 95
execute all the if statements and the last executed statement was the d
now in your switch statement case d
is not present so it's executes the default
one.
回答9:
You've gotten some answers already, but I think I'll suggest a slightly different possibility 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.
来源:https://stackoverflow.com/questions/7588364/switch-statement-c-help