I am sure this is quite simple, but I just want the program to end when the user enters -1. With the code I have below, it continues to display the highest, lowest, and average when a -1 is the first number entered. I thought maybe the answer was to move the cout into the for loop, but it just displayed the average, highest, and lowest as many times as the user entered a number. Probably fairly simple, but I'm a bit spent and need a nudge in the right direction. Thanks.
int main()
{
//sets up array for 75 user inputs
double scores[75];
//begins counter at -1 so counter starts at 0
int counter = -1;
do
{
counter++;
cout << "Please enter a score (enter -1 to stop): ";
cin >> scores[counter];
}
while (scores[counter] >= 0);
//declares appropriate variables and sets the boundaries for highest and lowest
double sum = 0, lowest = 100, highest = 0, average = 0;
//sets up the scenario for the counter array input
for (int x = 0; x < counter; x++)
{
//finds the highest number based on user input
if (scores [x] > highest)
{
highest = scores [x];
}
//finds the lowest number based on user input
if (scores [x] < lowest)
{
lowest = scores [x];
}
//sets up average calculation based on number of user inputs
if (sum += scores [x])
{
average = sum/counter;
}
}
//displays the appropriate outputs based on user input
cout << "Average is " << average << endl;
cout << "Highest is " << highest << endl;
cout << "Lowest is " << lowest << endl;
}
for (int x = 0; x < counter; x++)
{
//sets up average calculation based on number of user inputs
if (sum += scores [x])
{
average = sum/counter;
}
//finds the highest number based on user input
if (scores [x] > highest)
{
highest = scores [x];
}
//finds the lowest number based on user input
if (scores [x] < lowest)
{
lowest = scores [x];
}
if (scores [x] < 0)
{
break;
}
As you can see, I tried to enter a break statement, but when I do this the output is
Average is 0
Highest is 0
Lowest is 100
Did you mean I should add the =! -1 statement to the if scenarios?