[while loop] How to print average score after dropping highest and lowest scores?

Hello,

I am writing a program that will print the average score after dropping the highest and lowest inputted scores. I am trying to use the variables 'hi', 'lo', and 'sum', and was told by a friend to use 'count', which my IDE turns green like cout and cin for some reason (how can I use it?). So far I tried making the program to input as many test scores as the user wants, and when their finished, they'd enter a negative number to end the loop. My biggest problem, however, is that even if you input 30, 50, and 80, for instance, the output of the sum gives you 4309915. How can I accomplish these things? Thank you so much!

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
using namespace std;

int main()
{
    int grade, sum, hi, lo;

    cout << "Please enter your test scores and write a negative number when you're finished:\n";

    while(grade > 0)
        {
            cin >> grade;
            sum = sum + grade;
            grade++;
        }

    cout << "Your sum is " << sum;

    return 0;
}
You haven't initialised your variables, so they will start out as junk values.

1
2
3
4
5
6
    while(grade > 0)
    {
        cin >> grade;
        sum = sum + grade;
        grade++;
    }

If you enter in, for example, 0, it will be incremented to 1 and the loop will continue.
If you enter in, -10, that will be added to the sum, then the loop will terminate.
Oh! Of course!

Hmm.. out of curiosity, why was I getting such large numbers?

Also, how would I address the highest and lowest scores? I don't have the slightest clue how to do this. :/
Try using if statements. The first time through the loop give your hi and lo whatever the first grade is, then use if statement each time you run the loop to test if the new grade entered is greater than or less than the previous. At the end, you should have highest and lowest values.
Last edited on
Thank you for all your help!!!
Topic archived. No new replies allowed.