Averaging grades with multiple courses

I am trying to write a c++ code about averaging grades but with multiple courses.
I did write it but everytime i run it, it terminates right after i answer what course i want.
What am I doing wrong?

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  #include <iostream>

using namespace std;

int main()
{
    string course, A, B;
    
    float Average, Homework, Midterm1, Midterm2, Final;
    
    cout << "What course do you want? ";
    cin >> course;
    
    if (course == A)
    {
    
        cout << "Homework: ";
        cin >> Homework;
        cout << "Midterm 1: ";
        cin >> Midterm1;
        cout << "Midterm 2: ";
        cin >> Midterm2;
        cout << "Final: ";
        cin >> Final;
    
    cout << endl;
        
        Average = Homework*0.1 + Midterm1*.25 + Midterm2*.25 + Final*.4;
        
       if (Average < 60)
            {
                cout << "Your Average is " << Average << endl << "Your grade is F." << endl << "You failed." << endl;
            }
        else if ((Average >= 60) && (Average < 70))
            {
               cout << "Your Average is " << Average << "." << endl << "Your grade is D." << endl << "You passed." << endl;
            }
        else if ((Average >= 70) && (Average < 80))
            {
                cout << "Your Average is " << Average << "." << endl << "Your grade is C." << endl << "You passed." << endl;
            }
        else if ((Average >= 80) && (Average < 90))
               {
                  cout << "Your Average is " << Average << "." << endl << "Your grade is B." << endl << "Good work!" << endl;
               }
        else if ((Average >= 90) && (Average < 100))
               {
                  cout << "Your Average is " << Average << "." << endl << "Your grade is A." << endl << "Congratulations!" << endl;
               }
        else if ((Average < 0) || (Average > 100))
               {
                   cout << "Invalid entry." << endl << "Please try again" << endl;
               }
    }
    
    else if (course == B)
    {
        cout << "Homework: ";
        cin >> Homework;
        cout << "Midterm 1: ";
        cin >> Midterm1;
        cout << "Midterm 2: ";
        cin >> Midterm2;
        cout << "Final: ";
        cin >> Final;
        
        Average = Homework*0.15 + Midterm1*.25 + Midterm2*.25 + Final*.35;
        
        if (Average < 60)
            {
                cout << "Your Average is " << Average << "." << endl << "Your grade is F." << endl << "You failed." << endl;
            }
        else if ((Average >= 60) && (Average < 70))
            {
               cout << "Your Average is " << Average << "." << endl << "Your grade is D." << endl << "You passed." << endl;
            }
        else if ((Average >= 70) && (Average < 80))
            {
                cout << "Your Average is " << Average << "." << endl << "Your grade is C." << endl << "You passed." << endl;
            }
        else if ((Average >= 80) && (Average < 90))
               {
                  cout << "Your Average is " << Average << "." << endl << "Your grade is B." << endl << "Good work!" << endl;
               }
        else if ((Average >= 90) && (Average < 100))
               {
                  cout << "Your Average is " << Average << "." << endl << "Your grade is A." << endl << "Congratulations!" << endl;
               }
        else if ((Average < 0) || (Average > 100))
               {
                   cout << "Invalid entry." << endl << "Please try again" << endl;
               }
    }
    
    return 0;
}
Last edited on
> if (course == A)
Try it with
if (course == "A")

It looks like you don't know the difference between a variable name and a variable's value.

On line 14 A is the name of a std::string variable, that variable has a value of a empty string.

What you probably want is to be using a string literal "A" instead of the name of some "empty" variable. Also for future reference a string literal is denoted by double quotes "A", a character literal is denoted by single quotes 'A', and you defined a variable named A (no quotes) on line 7.

Make sure this is still doing what you are supposed to do, but a char instead of a string is good enough. And by the look of it you can throw out half the code by rearranging the course question.
It is always a good idea/practice to initialize variables, hence {0} or = 0 for Average etc.

floats are pretty much dead - use doubles :)

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

using namespace std;

int main()
{
    char course = '0'; // <--
    double Average{0}, Homework{0}, Midterm1{0}, Midterm2{0}, Final{0}; // <--
    
    cout << "What course do you want? ";
    cin >> course;
    
    cout << "Homework: ";
    cin >> Homework;
    cout << "Midterm 1: ";
    cin >> Midterm1;
    cout << "Midterm 2: ";
    cin >> Midterm2;
    cout << "Final: ";
    cin >> Final;
    
    cout << endl;
    
    if(course == 'A') // <--
        Average = Homework*0.1 + Midterm1*.25 + Midterm2*.25 + Final*.4;
    
    if(course == 'B') // <--
        Average = Homework*0.15 + Midterm1*.25 + Midterm2*.25 + Final*.35;
    
    if (Average < 60)
    {
        cout << "Your Average is " << Average << endl << "Your grade is F." << endl << "You failed." << endl;
    }
    else if ((Average >= 60) && (Average < 70))
    {
        cout << "Your Average is " << Average << "." << endl << "Your grade is D." << endl << "You passed." << endl;
    }
    else if ((Average >= 70) && (Average < 80))
    {
        cout << "Your Average is " << Average << "." << endl << "Your grade is C." << endl << "You passed." << endl;
    }
    else if ((Average >= 80) && (Average < 90))
    {
        cout << "Your Average is " << Average << "." << endl << "Your grade is B." << endl << "Good work!" << endl;
    }
    else if ((Average >= 90) && (Average < 100))
    {
        cout << "Your Average is " << Average << "." << endl << "Your grade is A." << endl << "Congratulations!" << endl;
    }
    else if ((Average < 0) || (Average > 100))
    {
        cout << "Invalid entry." << endl << "Please try again" << endl;
    }
    
    return 0;
}
Else \Else\, adv. & conj.
     2. Otherwise; in the other, or the contrary, case; if the
        facts were different.
        [1913 Webster]


1
2
3
4
if (condition)
   //...
else if (not condition) //useless test
   //... 
WOW!
Thank you so much!
I missed that!

But I just realized that the code is a bit long
Is there any way I can clean it up and write it shorter?
When you have repeated code, it's often better to create a table of data and process the table. Building on Againtry's solution:
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
#include <iostream>

using namespace std;

int main()
{
    char course = '0'; // <--
    double Average{0}, Homework{0}, Midterm1{0}, Midterm2{0}, Final{0}; // <--
    
    cout << "What course do you want? ";
    cin >> course;
    
    cout << "Homework: ";
    cin >> Homework;
    cout << "Midterm 1: ";
    cin >> Midterm1;
    cout << "Midterm 2: ";
    cin >> Midterm2;
    cout << "Final: ";
    cin >> Final;
    
    cout << endl;
    
    if(course == 'A') // <--
        Average = Homework*0.1 + Midterm1*.25 + Midterm2*.25 + Final*.4;
    
    if(course == 'B') // <--
        Average = Homework*0.15 + Midterm1*.25 + Midterm2*.25 + Final*.35;
    
    struct GradeData {
	double ceiling;
	const char *grade;
	const char *comment;
    };
    GradeData data[] = {
	{ 60, "F", "You failed." },
	{ 70, "D", "You passed." },
	{ 80, "C", "You passed." },
	{ 90, "B", "Good work!" },
	{ 100, "A", "Congratulations" },
	{ 0, nullptr, nullptr }	// marks end of array
    };

    if ((Average < 0) || (Average >= 100))
    {
        cout << "Invalid entry." << endl << "Please try again" << endl;
    } else {
	// If you get here then the grade must be within the table
	for (int i=0; data[i].grade != nullptr; ++i) {
	    if (Average < data[i].ceiling) {
		cout << "Your Average is " << Average << endl
		     << "Your grade is " << data[i].grade << ".\n"
		     << data[i].comment << endl;
		break;
	    }
	}
    }
    
    return 0;
}

It's an error to use floating-point numbers in this situation. Floating point numbers are only approximations, but this program doesn't work properly in every case unless math is exact. Quantization error could cause the code to report the wrong average. This is especially problematic if the computer approximates the the user's average as a value slightly larger than 100.

It's also unlikely that a student can score fractional points (e.g., get a grade of 91.234) on their assignments, so give the scores type int.

Once that's done, validate all your input by making sure it's in [0, 100]. Only once the input is verified, you can compute the weighted average from plain integers like this:
1
2
3
    int const average = (course == 'A')
      ? ((hw * 10) + (test1 * 25) + (test2 * 25) + (final_exam * 40)) / 100
      : ((hw * 15) + (test1 * 25) + (test2 * 25) + (final_exam * 35)) / 100;

Last edited on
Topic archived. No new replies allowed.