Find average of grades and find largest value (c++)

  average, c++, loops, while-loop

So I need to find the average of x amount of students’ grades (its a loop). It will keep adding until you enter -1. I got the average correct but to find the largest value is where the problem is at. It always prints out a garbage value instead of the largest value out of the list

Example input-output:
Enter the gades (Enter -1 when done): 65 48 83 93 -1
The maximum grade is 93 and the average is 72.25

here is the code I have so far

#include <iostream>

using namespace std;

int main()
{
    double grades = 0;
    double sum = 0;
    int counter = 0;
    int largest;

    cout << "Enter the grades: "<<endl;
    
    for (int amount = 1; ; amount++){

         while (counter < amount)

              counter++;

           cin >> grades;
        
          if(grades !=-1)
        {
            sum += grades;

            
        if (grades > largest)
        {
        largest = grades;

    }


        else
        

            cout << "The maximum grade is "<< largest <<" and the average is " << sum/(amount-1) << endl;
            break;
        }
    }
    
    return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT