switch statment

this works but whats better way to do this? like is there anyway to just have the user type A,B,C,D, or F or am I asking to much ?

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()
{
   int grade;
   cout << "enter you grade"<< endl;
   cout << " 1 = A, 2 = B, 3 = C, 4 = D, 5 = F" << endl;
   cin >> grade;

   switch (grade){
   case 1:
       cout << "you got a great grade" << endl;
       break;
   case 2:
        cout  << " you got an ok grade"<< endl;
        break;
   case 3:
        cout << "you got an average grade" << endl;
        break;
   case 4:
        cout << "not so hot"<< endl;
        break;
   case 5:
        cout << "you failed"<< endl;
        break;
   default:
        cout << "not valid grade"<< endl;


   }
    return 0;
}
That is a perfectly good way to do that.
now how would I write it if I needed to have the user input there score and output what grade they got so far I have this. I know its not right and I might just be being dumb.
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
48
49
50
#include <iostream>
#include <cmath>


using namespace std;

int main()
{
   int grade, score;
   cout << "enter you grade"<< endl;
   cout << " 1 = A, 2 = B, 3 = C, 4 = D, 5 = F" << endl;
   cin >> grade;
   if (score >=90){
    cout << "you got an A"<< endl;
   }else if ((score <90) && (score >=80){
             cout << " you got a B"<< endl;
             }else if ((score <80)&& (score >=70)){
             cout << " you got a C" << endl;
             } else if ((score <70)&&(score >=60)){
             cout << "you got a D"<< endl;
             }else (score <60){
             cout << " you failed"<< endl;
             }
   grade = score;


   switch (grade){
   case 1:
       cout << "you got a great grade" << endl;
       break;
   case 2:
        cout  << " you got an ok grade"<< endl;
        break;
   case 3:
        cout << "you got an average grade" << endl;
        break;
   case 4:
        cout << "not so hot"<< endl;
        break;
   case 5:
        cout << "you failed"<< endl;
        break;
   default:
        cout << "not valid grade"<< endl;


   }
    return 0;
}
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
closed account (48T7M4Gy)
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
thanks I think I got it now

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
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cmath>


using namespace std;

int main()
{
   int grade, score;
   cout << "enter you score"<< endl;

   cin >> score;
   if (score >=90){
    cout << "you got an A"<< endl;
   }else if ((score <90) && (score >=80)){
             cout << " you got a B"<< endl;
             }
             else if ((score <80)&& (score >=70)){
             cout << " you got a C" << endl;
             }
             else if ((score <70)&&(score >=60)){
             cout << "you got a D" << endl;
             }
             else if (score <60){
             cout << " you failed"<< endl;
             }
               grade = score;
             cout << "enter your grade" << endl;
              cout << " 1 = A, 2 = B, 3 = C, 4 = D, 5 = F" << endl;
             cin >> grade;


   switch (grade){
   case 1:
       cout << "you got a great grade" << endl;
       break;
   case 2:
        cout  << " you got an ok grade"<< endl;
        break;
   case 3:
        cout << "you got an average grade" << endl;
        break;
   case 4:
        cout << "not so hot"<< endl;
        break;
   case 5:
        cout << "you failed"<< endl;
        break;
   default:
        cout << "not valid grade"<< endl;


   }
    return 0;
}

closed account (48T7M4Gy)
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
thanks that does look a lot cleaner
Topic archived. No new replies allowed.