What am I missing?

Write a program that reads in letter grades for a class of 20 students and displays
the number of students who passed (D or better) and the number who failed. (F)

How do I get the it to cout the students who passed or failed?
Thanks.

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
int main()

{
    char X;
    int pass, fail = 0;

    cout<<"Enter a grade: ";

    for(int i = 0; i < 20; i++)
        {

            cin>>X;
            switch(X){
                case 'A':
                    pass++;

                case 'B':
                    pass++;

                case 'C':
                    pass++;

                case 'D':
                    pass++;

                case 'F':
                    fail++;


                default:
                    fail++;


            }

    }

    return(0);
}
Last edited on
cout << pass << endl;
cout << fail << endl;
Last edited on
Hello Extinqt,

In addition to SamuelAdams suggestion you are missing "break;" statements in the case statements. Although what you have will work it gives you incorrect totals for pass and fail. Also the "default" case should not be an addition to "fail", but a message saying something about the wrong letter being entered along with subtracting 1 from "i" to give the user a chance to reenter the grade.

I would also consider moving line 7 to line 11.

You could also write the switch as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch (X)
{
case 'A':
case 'B':
case 'C':
case 'D':
	pass++;
	break;
case 'F':
	fail++;
	break;
default:
	std::cout << "Invalid Entry. Choices are: A B C D and F";
        i--;  //  Allows the user to correct the invalid entry.
} // end switch 

A Shorter way to write the switch, but still works the same.

Hope that helps,

Andy
Topic archived. No new replies allowed.