Grading Program and Boolean Variable Confusion!

Okay Im a newbie and Im working on a program that allows the user to enter a grade (0-100) and then the console returns to the user the grade and the letter of the grade. This is what i have and so far I have only programmed grades 70 and up. Here's my problem: if the grade is 90 or above, the console displays "A".. which is fine. But when I enter a grade that is less than 90 and above 80, the console displays "AB".. then when I enter a grade above 70 and less than 80 the console displays "ABC".. this program is supposed to return grades, not teach me my ABCs. Please help and let me know if I am approaching this program completely wrong. Here's what I got:

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
#include <iostream>

using namespace std;

int main()
{
    bool P, A, B, C, D, F;
    P = false;
    A = false;
    B = false;
    C = false;

    int grade;
    cout << "Enter Grade:\n";
    cin >> grade;
    if (grade == 100)
    {
        P = true;
        if (P = true)
        {
            cout << "Perfect Score!";
        }
    }
    if (grade >= 90, grade < 100)
    {
        A = true;
        if (A = true)
        {
            cout << "A";
        }
    }
    if (grade >= 80, grade < 90)
    {
        B = true;
        if (B = true)
        {
            cout << "B";
        }
    }
    if (grade >= 70, grade < 80)
    {
        C = true;
        if (C = true)
        {
            cout << "C";
        }
    }

    return 0;
}
Replace
if (grade >= 90, grade < 100)
with
if (grade >= 90 && grade < 100)

The comma gets you to do two seperate operations. It doesn't replace the AND operator.

Note:
if (B = true) is probably not doing what you think it is doing. it is setting B equal to true (not comparing) and then returning that value (which is true).
Note 2:
Why would you set B = true; then if (B == true)? It seems unnessesary since the only times that you'll enter the for loop is if the line in front of it is called which always happens.
Last edited on
Also you can simplify your code for example


1
2
3
4
5
6
7
8
    if (grade >= 90, grade < 100)
    {
        A = true;
        if (A = true)
        {
            cout << "A";
        }
    }


the following way


if ( A = ( grade >= 90 && grade < 100 ) ) cout << "A";
Topic archived. No new replies allowed.