Jan 25, 2017 at 3:05am UTC
That is a perfectly good way to do that.
Jan 25, 2017 at 4:13am UTC
You have the right idea! Couple things you need to check though. First, the user needs an opportunity to enter the score. If you have the user only input score you will have to have your code assign a number to grade within the if statements so when it exits, the switch has a number to test. Second, check your syntax. Your first else if is missing a )
. Keep working at it though you have the right idea!
Last edited on Jan 25, 2017 at 4:14am UTC
Jan 25, 2017 at 4:20am UTC
Response to OP only, not later calculation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <iostream>
//#include <cmath>
using namespace std;
int main()
{
char grade;
cout << "enter you grade" << endl;
//cout << " 1 = A, 2 = B, 3 = C, 4 = D, 5 = F" << endl;
cin >> grade;
switch (toupper(grade)){
case 'A' :
cout << "you got a great grade" << endl;
break ;
case 'B' :
cout << " you got an ok grade" << endl;
break ;
case 'C' :
cout << "you got an average grade" << endl;
break ;
case 'D' :
cout << "not so hot" << endl;
break ;
case 'F' :
cout << "you failed" << endl;
break ;
default :
cout << "not valid grade" << endl;
}
return 0;
}
enter you grade
1 = A, 2 = B, 3 = C, 4 = D, 5 = F
f
you failed
Program ended with exit code: 0
Last edited on Jan 25, 2017 at 4:43am UTC
Jan 25, 2017 at 4:42am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <iostream>
using namespace std;
int main()
{
int score;
char grade;
cout << "enter you score: " ;
//cout << " 1 = A, 2 = B, 3 = C, 4 = D, 5 = F" << 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 << "Your grade is: " << grade << " - " ;
switch ( toupper(grade) )
{
case 'A' :
cout << "you got a great grade" << endl;
break ;
case 'B' :
cout << " you got an ok grade" << endl;
break ;
case 'C' :
cout << "you got an average grade" << endl;
break ;
case 'D' :
cout << "not so hot" << endl;
break ;
case 'E' :
cout << "you failed" << endl;
break ;
default :
cout << "not valid grade" << endl;
}
return 0;
}
Or:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <iostream>
#include <string>
using namespace std;
int main()
{
int score;
char grade;
string comment;
cout << "enter you score: " ;
cin >> score;
if ( score >=90 ) {
grade = 'A' ;
comment = "you got a great grade\n" ;
}
else if ( score >= 80) {
grade = 'B' ;
comment = "you got an ok grade\n" ;
}
else if ( score >= 70 ) {
grade = 'C' ;
comment = "you got an average grade\n" ;
}
else if ( score >= 60 ) {
grade = 'D' ;
comment = "not so hot\n" ;
}
else {
grade = 'F' ;
comment = "you failed\n" ;
}
cout << "Your grade is: " << grade << " - " << comment;
return 0;
}
Last edited on Jan 26, 2017 at 2:37am UTC
Jan 25, 2017 at 5:02am UTC
thanks that does look a lot cleaner