Range Validation and Letter Grade Output issues

Hello. I'm new here and with C++ and I'm trying to simulate finding the overall grade of a student in a semester, if that makes sense. I'm using Cygwin to run my program. There are two problems I'm having, though.

1. The "Your answer is invalid." call doesn't print until after all five grades have been typed in by the user. My intention was for it to print "Your answer is invalid." after I put in a value that is not between 1 and 100 for one of my grades, and possibly ask the user to type in a more accurate grade as well.

2. Every time I input five reasonable grades, my average letter grade always ends up as an "A+" when that shouldn't be the case.

Here is my 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
63
64
65
#include <iostream> //includes the header file iostream
using namespace std; //using standard names from iostream
int main() //main function
{
int grade1;
int grade2;
int grade3;
int grade4;
int grade5;
int final;
cout << "What is your first grade? ";
cin >> grade1;
cout << "What is your second grade? ";
cin >> grade2;
cout << "What is your third grade? ";
cin >> grade3;
cout << "What is your fourth grade? ";
cin >> grade4;
cout << "What is your fifth grade? ";
cin >> grade5;
final = (grade1 + grade2 + grade3 + grade4 + grade5)/5;
cout << final << endl;
if ((grade1 > 100 ) || (grade1 < 1)) {
		cout << "Your answer is invalid. \n" << endl;
}
if ((grade2 > 100 ) || (grade2 < 1)) {
		cout << "Your answer is invalid. \n" << endl;
}
if ((grade3 > 100 ) || (grade3 < 1)) {
		cout << "Your answer is invalid. \n" << endl;
}
if ((grade4 > 100 ) || (grade4 < 1)) {
		cout << "Your answer is invalid. \n" << endl;
}
if ((grade5 > 100 ) || (grade5 < 1)) {
		cout << "Your answer is invalid. \n" << endl;
}

if (91 <= final <= 100) 
{
		cout << "A+ ";
}
else if (81 <= final <= 90.99) 
{
		cout << "A ";
}
else if (71 <= final <= 80.99) 
{
		cout << "B ";
}
else if (61 <= final <= 70.99) 
{
		cout << "C ";
}
else if (51 <= final <= 60.99) 
{
		cout << "D ";
}
else
{
		cout << "Fail ";
}

return 0;
}


Can anyone see the issues with my 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
#include <iostream>

// get grade from stdin in the range [0,100]
int get_grade( int n )
{
    std::cout << "enter your grade #" << n << " [0,100]: " ;

    int grade ;
    if( std::cin >> grade ) // if the user  entered an int
    {
        // return it if it is in the valid range
        if( grade >= 0 && grade <= 100 ) return grade ;

        // entered int is out of range
        std::cout << "error: input is out of range. please try again\n" ;
    }

    else // a grade was not entered
    {
        std::cout << "error: the input is not an integer. try again\n" ;
        std::cin.clear() ; // clear the failed state of the stream
        std::cin.ignore( 1000, '\n' ) ; // extract and discard the bad input
    }

    return get_grade(n) ; // try again (call the same function once more)
}

int main()
{
    const int grade_1 = get_grade(1) ;
    const int grade_2 = get_grade(2) ;
    const int grade_3 = get_grade(3) ;
    const int grade_4 = get_grade(4) ;
    const int grade_5 = get_grade(5) ;

    const int total = grade_1 + grade_2 + grade_3 + grade_4 + grade_5 ;
    const double final_grade = total / 5.0 ; // note: 5.0 avoid integer division

    if( final_grade >= 91 ) std::cout << "A+\n" ;
    else if( final_grade >= 81 ) std::cout << "A\n" ; // >= 81 and < 91
    else if( final_grade >= 71 ) std::cout << "B\n" ; // >= 71 and < 81
    else if( final_grade >= 61 ) std::cout << "C\n" ; // etc.
    else if( final_grade >= 51 ) std::cout << "D\n" ;
    else std::cout << "Fail\n" ;
}
Hello!

The main problem revolves around this line of code:

1.] final = (grade1 + grade2 + grade3 + grade4 + grade5)/5;

You are calculating the average. Later in the code, in your if/else statements, this inevitably leads to two issues. One has to do with the value that your final variable holds. The other has to do with the way you have written the statements.

2.] if (91 <= final <= 100)

First of all, if you write it like this, which should rather be

if (final == 100)
{
....
}
else if (final >= 90)
{
....
}

only this first statement will execute, hence A+ is output all the time. The reason is, that, no matter how high the sum of your grades, since you calculate the average, your number will always be lower than or equal to 100. And lower than any passing grade besides. So, the first thing to do is to change:

final = (grade1 + grade2 + grade3 + grade4 + grade5);

To only hold the sum of all grades. The next step is to change your if/else statements according to my example. If you further wish to have someone using your program input a value if the original value is invalid, add this to your code:

1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "What is your first grade? ";
cin >> grade1;

while (grade1 < 0 || grade1 > 100)
{
    cout << "Error: Enter a valid grade: ";
    cin >> grade1;
}

cout << "What is your second grade? ";
cin >> grade2;

// so on so forth 


If you wish to calculate the average of all grades, you should add another variable, called averageGrade. (Or whatever you wish to name it). To calculate the average then all you need to do is write:

averageGrade = final / 5;
cout << "Your average grade is: " << averageGrade << endl;

What you should do besides that is to initialize your variables. You can't assume that all of them will receive a default value of 0, which can lead to wrong results in calculations. ;-)
Last edited on
To only hold the sum of all grades. The next step is to change your if/else statements according to my example. If you further wish to have someone using your program input a value if the original value is invalid, add this to your code:


Is there a way to do this and the letter grade without using the "while" call and keeping it as an if/else statement, Misenna?
Last edited on
In case you are asking: Can you keep the input validation as an if/else statement, yes you can. :)

Assuming that your input validation looks like this:

1
2
3
4
5
6
7
8
9
10
if (grade1 < 0 || grade1 > 100)
{
    cout << "Error: Invalid grade.\n";
    cout << "Enter grade1: ";
    cin >> grade;
}
else if (grade2 < 0 || grade2 > 100)
{
   // code
}


The drawback of not using a while-loop is, that your user will not be asked more than once to provide a valid input. If the value is still invalid, the program will continue to the next statement, leading to wrong results in your calculation of the sum of all grades, and potentially also the average of all grades. There is a way to deal with such situations using an if/else statement of course! Consider this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if (grade2 < 0 || grade2 > 100)
{
    cout << "Error: Invalid grade.\n";
    cout << "Enter grade2: ";
    cin >> grade;
}
else
{
     // You are assigning the grade2 variable a valid value
    grade2 = 0;
}

if (grade3 < 0 || grade3 > 100)
{
    // code
}
else
{
    grade3 = 0;
}


For the determination and output of the appropriate letter-grade, JLBorges has given you an excellent example. Use this. :)
Last edited on
Thank you so much for your help Misenna!
Topic archived. No new replies allowed.