dimanche 17 octobre 2021

Number is adding itself each time to itself [duplicate]

I'm currently doing a school project where I need to find an average rating of an array, while I'm not having a problem getting the average number if I try to get the average number again it is adding itself onto the previous average. ex: input

3 //this is to get the average rating to display from the menu

output

4

input

3

output

8

Here is my code to display the avg rating


void displayAverageRating(int ratings[NUM_PEOPLE]) {
  for (i = 0; i <= 835; i++) {
    sum += ratings[i];
    avgrating = sum / NUM_PEOPLE;
    if (i == 835) {
      cout << "The Average of the ratings for Star Wars: Episode I The Phantom "
              "Menace was ";
      cout << avgrating << "\n";
    }
  }
}

Here are my menu options

do {
    getMenu();
    cin >> Menu_Choice;
    switch (Menu_Choice) {
        case 1:
            displayRatings(ratings);
            break;
        case 2:
            ChangeARating(ratings);
            break;
        case 3:
            displayAverageRating(ratings);
            break;
    }
}
while (Menu_Choice !=4);

return 0;

This is my first post so if I did anything wrong please lmk! Thanks :)

Aucun commentaire:

Enregistrer un commentaire