This program is to calculate the average, high and low score of a series of use inputs. The output I am getting is high, low, average, high, low. I have no idea why or how this can happen. Side note, I am aware the coding isn't very good but the program otherwise works.

int main () // calculates test scores, printing high, low, and average.
{
	double scores[75];
	int counter = -1;
	do 
	{
		counter++;
		cout << "Please enter a score (enter -1 to stop): ";
		cin >> scores[counter];
	} while (scores[counter] >= 0);
	double total = 0.0;
	for (int y = 0; y < counter; y++)
	{
		if (y >= 0)
			total += scores[y];
		else 
		{
		}
	}
	double high = 0.0, low = 75.0;
	for (int x = 0; x < counter; x++)
	{
		double score = scores[x];
		if (x < 0)
		{
		}
		if ((score >= 0)&&(score > high))
			high = score;
		if ((score >= 0)&&(score < low))
			low = score;
		else
			cout << "Average is " << (total / (double) (counter)) << endl;
			cout << "Highest is " << high << endl; 
			cout << "Lowest is " << low << endl;
	}
}

Here are some observations

1) You should definitely use an std::vector<double> instead of a fixed length array. There is no reason to arbitrarily restrict the user to 75 inputs. If you don't change this, you should certainly check if the counter goes above 75 and throw an error if it does.

2) You are starting y at 0 and only incrementing it, so it will never be zero, so you don't have to check if(y>=0) 3) You should not store the "-1 to stop" in the array. You should get the value from the user and only put it in the array if it is > 0.

4) x will never be less than zero.

5) If you do #3, 'score' will never be < 0.

6) Your actual problem is that you are doing the output inside the for loop. You should move the output to the very end of the program (outside of the loop).

Hope that helps,

David

I need the output inside the loop because I don't want it to print if the user types -1 from the start. Going to fiddle with the other things a tad.

You can do

for (int x = 0; x < counter; x++)
	{
		double score = scores[x];
		if (x < 0)
		{
		}
		if ((score >= 0)&&(score > high))
			high = score;
		if ((score >= 0)&&(score < low))
			low = score;
		else
                        break;
	}

cout << "Average is " << (total / (double) (counter)) << endl;
cout << "Highest is " << high << endl; 
cout << "Lowest is " << low << endl;

Also, setting low = 75.0; is not a safe assumption. What if all of the scores are above 75?

David

Test totals are out of 75 points so that part is fine.

Ok tried your suggestion but it still prints out the totals when I use -1 from the start. If I put it in the loop though it doubles the output of high and low for some reason.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.