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