Making a program end without displaying the output

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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;	
}
If the first input is -1, the value of counter is 0.
If you put the output into an if-request like
1
2
3
4
if(counter > 0)
{
  ...
}
it's not done for only one input.


If you want no output if the input is directly -1, the if-request could look like this:
1
2
3
4
if(scores[counter] != -1)
{
  ...
}
it's not done for only one input.
Last edited on
Thanks...unforunately, I can't modify the do while scenario at the top that sets counter greater than or equal to 0, which is a pain.

So the challenge is ending the program without the program displaying:

The average is ...blah
The highest is...blah
The lowest is...blah..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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?
Try this:
1
2
3
4
5
6
if(counter > 0)
{
  cout << "Average is " << average << endl;
  cout << "Highest is " << highest << endl;
  cout << "Lowest is " << lowest << endl;	
}


so it only makes an output if more than one number was entered.
Last edited on
Sweet...that makes sense. Huge help...So it executes as long as a counter number is entered.

Thanks for your help. Don't know why I didn't see that, but I've been staring at this a while.
Topic archived. No new replies allowed.