Grade Sum and Average Calculator: Console Application

Hi Everyone, I'm tasked with an assignment to build a program that calculates the average and sum of grades entered by a user. Instructions from the professor are as follows,

You are to write a program that would help me compute average grade of a student. I would like to enter a set of grades into the program, I will let the program know that I am done entering the grades by entering -1 as the grade. So until I enter -1, the program should keep asking me for another grade to enter. Once I am done entering grades, the program should output the average grade and sum of the grades. Make sure that you are using for loop for this program (no while loop). My current code looks as below. I need to find a way to terminate the loop when a user enter -1 and it should prompt to view the average grade and the sum of the grades that was entered by the user. Can someone guide me with this assignment? I appreciate any help. I managed to get it working until the processing part where I'm lost as to how I could use the variables correctly to calculate the average and sum. 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
 #include <iostream>
using namespace std;

int main()
{
	//INPUT - Input any amount of grades, and enter -1 to indicate the end:
	int grades;
	double total = 0;

	for (int NumGrades = 0;; NumGrades++)
	{
		cout << "Enter the Grade " << (NumGrades+1) << ": ";
		cin >> grades;
		//total of the grades
		total += grades;
		if (grades == -1) {
			int GradeSum = grades;
			int GradeAv = grades / NumGrades;
			cout << "Sum of Grades: " << GradeSum << endl;
			cout << "Average of Grades: " << GradeAv << endl;
		}


	}
Last edited on
Instead of a for loop try using while loop.
1
2
3
4
5
6
while(number != -1){
   // Compute the grades
   // Find the average of the grades
   // Increment the number of grades the user has input
   // Ask the user for the value
}
Make sure that you are using for loop for this program (no while loop).


Unfortunately, the professor wants us to avoid the use of while loop for this assignment.
You can use break to get out the loop

1
2
3
4
5
6
for(int i  = 0; ; i++){
    cin >> number;
    if(number == -1){ break; }
    else
        // Computation
}
Last edited on
Thank you for the guidance. I have one more question, when I run the current code that I have I don't get the sum and the average correctly. If it's still under the forum rules, would you be able to tell me what I'm doing wrong?
Last edited on
Well the sum should be total but you set it as grades which is incorrect. Inside your for loop, you don't have to set NumGrades to 0, you can set it to 1 because without the condition, it'll loop infinitely. When you try to get the GradeAv of your first number, it'll try to divide by 0 which will probably give garbage value.
Thank you! I haven't calculated the average in a logical manner either. Coding isn't easy but I like this challenge. Thank you so much for putting me on the right track.
Last edited on
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

int main()
{
    //INPUT - Input any amount of grades, and enter -1 to indicate the end:
    int grades = 0;
    double total = 0;
    
    for (int NumGrades = 1; ; NumGrades++)
    {
        cout << "Enter the Grade " << (NumGrades) << ": ";
        cin >> grades;
        
        if(grades !=-1)
        {
            //total of the grades
            total += grades;
        }
        else
        {
            cout << "Sum of Grades: " << total << endl;
            cout << "Average of Grades: " << total/(NumGrades-1) << endl;
            break;
        }
    }
    
    return 0;
}
closed account (48T7M4Gy)
Even though it does the same thing a while loop lends itself better to this problem:
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
#include <iostream>

using namespace std;

int main()
{
    //INPUT - Input any amount of grades, and enter -1 to indicate the end:
    int grades = 0;
    double total = 0;
    int NumGrades = 1;
    
    while(
          cout << "Enter the Grade " << NumGrades << ": "
          && cin >> grades
          && grades != -1
          )
    {
        total += grades;
        NumGrades++;
    }
    
    cout << "Sum of Grades: " << total << endl;
    cout << "Average of Grades: " << total/(NumGrades - 1) << '\n';
    
    return 0;
}
Thank you so much. Your code helped me immensely to clarify what I was doing wrong. It's great that I found out about the not equal operator because it will come in handy. Thanks again!
Topic archived. No new replies allowed.