Grading Program

Hello all!

I've been getting back into C++ but instead of re-learning the material, I decided to look up exercises and see where I stand. Apparently, I'm sitting.

The exercise was to write a program using a switch that will take a numeric score and return a letter grade. When I enter the score, it skips straight to "Press any key to continue..."

Please don't fix my errors; I'd like to fix my own mistakes. I'm just not sure if the problem is in the way I wrote the switch statement or the voids (or both >_<). It may also be my if statements but, I'm inclined to say that I've done those correctly. Here's the code.
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
58
59
60
61
62
#include <cstdlib>
#include <iostream>

using namespace std;

 int score;
 


void pass()
{
     if (score == 100)
     {
               cout << "Congratulations! You got a perfect score! /n";
               }
     else if (score >= 90, score <= 99)
     {
          cout << "Congratulations! You got an A! /n";
          }         
     else if (score >= 80, score <= 89)
     {
          cout << "Not bad! You git a B! /n";
          }
     else if (score >= 70, score <= 79)
     {
          cout << "Keep working! You got a C! \n";
          }
     else if (score >= 60, score <= 69)
     {
          cout << "You need to practice! You got a D! /n";
          }
          
}

void fail()
{
     if (score <= 59)
     {
               cout << "You failed! Try again!";
               }
               
}
              
int main()
{
       
    cout << "Please input your score: ";
    cin >> score;
    
    switch (score)
    {
           case 1:
                pass();
                break;
           case 2:
                fail();
                break;
                }
    
    system ("PAUSE");
    return 0;
}


Thanks for any advice!
-Cob
Last edited on
Please use [code][/code] tags.

Try describing the program (not to me, in your head or out loud to yourself), line by line, function by function. Compare that to what you want to happen, then you should find the errors (that's similar to how compilers work idn't it? the 'd' was intentional).

To get you started, the problem is your switch statement. It isn't doing what you think it is. It only accepts two values, 1 and 2, one takes it to the pass () function, which doesn't have anything in the if else statement for anything under 60.

Your if else statement is another thing. if (condition one, condition two) is wrong, don't use a comma to separate conditions. It should be || (or), && (and) and ! (not) (though I am not sure if it can be used in such a sense). So:

if (a == A && A == a)

would be "if 'a' is equal to 'A' and 'A' is equal to 'a'"

Try reading this:

http://cplusplus.com/doc/tutorial/operators/
A switch statement is not what you want to check ranges. Switch works for a set of discreet values. Like:
1
2
3
4
5
6
7
8
9
10
switch(day) {
 case 0: cout << "Sunday"; break;
 case 1: cout << "Monday"; break;
 case 2: cout << "Tuesday"; break;
 case 3: cout << "Wednesday"; break;
 case 4: cout << "Tursday"; break;
 case 5: cout << "Friday"; break;
 case 6: cout << "Saturday"; break;
 default: cout << "NaD";
}
However, this could also be written a bit differently using arrays.
1
2
3
4
5
6
7
8
9
const int N = 7;
char *days[] = {
	"Sunday", "Monday", "Tuesday", "Wednesday",
	"Tursday", "Friday", "Saturday"
};
if( day < 0 || day >= N )
	cout << "NaD";
else
	cout << days[day];

However your idea of a series of if, else-if, else statements is the way you want to go.
Last edited on
listen buddy i guess the variable inside switch brackets can never have the value 1 ... And then call the function pass ... just think.... there must be an if-else statement specifying whether the student is pass or fail and then call the functions....pass or fail
Thanks all for the help! Hopefully as i work on these types of exercises, I'll start to put my knowledge back in order!

@Danny Toledo: Sorry about that! I totally didn't even think about the code brackets! And for correcting my conditionals, I knew the comma was wrong but I didn't even think to look it up! >_<

edit: Am I getting closer by assuming the issue is the relationship between the integer "score" and the cases?
Last edited on
Yes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
switch(val) {
 case 1:
    a();
    break;
 case 2:
    b();
    break;
 case 3:
    c();
    break;
 ...
 default:
    z();
}
//is the same as
if( val == 1 )
    a();
else if( val == 2 )
    b();
else if( val == 3 )
    c();
...
else
    z();
I fixed the problem and got the grading program working! I meant to post it last week when it happened and thanks to all for helping!
Topic archived. No new replies allowed.