0

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;
}
Cyrus
  • 49
  • 1
  • 1
  • 7
  • [Don't use `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – SuperStormer Mar 02 '21 at 02:11

1 Answers1

1

You forgot to initialize largest.

Also your indentation and brackets need to be reviewed to ensure they are balanced. Right now it looks a right mess.

Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
  • 1
    Worse than a right mess. I think my anonymous friend may be smurfing themselves over with the bad indentation. If I'm not mistaken `while (counter < amount)` is only looping `counter++;` and if that's the case they might as well `counter = amount;` and be done with it. – user4581301 Mar 02 '21 at 01:48