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!
#include <iostream>
usingnamespace 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;
}
}
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
}
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?
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.
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!