While loop for decision making

Hello guys, I'm coding a program where the student will enter their mark and the program will display their grade. For the students' ease, I want to let them key in another mark if they put 'Y' or 'y' on the question, "Do you want to enter another mark?". And the program will exit if the user enter 'N' or 'n'.

Is it necessary for me to include the variable 'decision'?
Where do I put the cout, "Do you wish to enter another mark?"?
Is the logical operator location is correct?

If there is other problem than what I know I face, please point it out. Thank you.
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
#include <iostream>

using namespace std;

int main()
{
    char grade1, grade2, grade3, grade4, grade5, decision;
    double mark;

    grade1 = 'A';
    grade2 = 'B';
    grade3 = 'C';
    grade4 = 'D';
    grade5 = 'E';

    while(decision == 'Y' || decision == 'y')
    {

    cout << "Please enter your mark for Mathematics HL exam in the range of 0-100" << endl;
    cin >> mark;

    if(mark <= 100 && mark >= 80)
    {
        cout << "Your grade for this subject is: " << grade1 << endl;
        cout << "Congratulation! Please keep your hardwork!" << endl;
    }

    if(mark < 80 && mark >= 65)
    {
        cout << "Your grade for this subject is: " << grade2 << endl;
        cout << "You did well in the exam. Strive for the next exam." << endl;
    }

    if(mark < 65 && mark >= 50)
    {
        cout << "Your grade for this subject is: " << grade3 << endl;
        cout << "Practice make perfect! :D" << endl;
    }

    if(mark < 50 && mark >= 40)
    {
        cout << "Your grade for this subject is: " << grade4 << endl;
        cout << "Please improve your grade for the next exam." << endl;
    }

    if(mark < 40)
    {
        cout << "Your grade for this subject is: " << grade5 << endl;
        cout << "Please come and see me after this. >(" << endl;
    }

    cout << "Do you wish to enter another mark?" << endl;
    cin >> decision;

    }

    return 0;
}
Line 7: decision is an uninitialized variable (garbage)

Line 16: You're testing a garbage variable on the first iteration. Chances are decision is not 'Y' or 'y', therefore you loop will never execute. A do/while loop is more appropriate here. A do/while loop will test the continuation condition at the bottom of the loop.



Everything is correct, just replace while loop with do while loop.
'decision' is necessary for the loop to be working.
grade1-grade5 in this situation is not that useful, just put A-E inside cout.


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

#include <iostream>

using namespace std;

int main()
{
    char decision;
    double mark;

    do//use a do while loop instead of while loop ;)
    {

    cout << "Please enter your mark for Mathematics HL exam in the range of 0-100" << endl;
    cin >> mark;

    if(mark <= 100 && mark >= 80)
    {
        cout << "Your grade for this subject is: A" << endl;
        cout << "Congratulation! Please keep your hardwork!" << endl;
    }

    if(mark < 80 && mark >= 65)
    {
        cout << "Your grade for this subject is: B" << endl;
        cout << "You did well in the exam. Strive for the next exam." << endl;
    }

    if(mark < 65 && mark >= 50)
    {
        cout << "Your grade for this subject is: C" << endl;
        cout << "Practice make perfect! :D" << endl;
    }

    if(mark < 50 && mark >= 40)
    {
        cout << "Your grade for this subject is: D" << endl;
        cout << "Please improve your grade for the next exam." << endl;
    }

    if(mark < 40)
    {
        cout << "Your grade for this subject is: E" << endl;
        cout << "Please come and see me after this. >(" << endl;
    }

    cout << "Do you wish to enter another mark?" << endl;
    cin >> decision;

    }while(decision == 'Y' || decision == 'y');

    return 0;
}
Topic archived. No new replies allowed.