Greatest\Least number

I am writing a program from a book where it finds the greatest and least numbers. Technically it does work just doesn't work how it should. It grabs the greatest and least number but when I hit -99 it still takes that number as num which I understand why it is doing it but not sure how to fix it. I tried taking the input out of the do while, tried just a while but neither seem to work. I know it is probably something simple just cant figure it out.

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
  #include<iostream>
using namespace std;

int main()
{
	//Declare variables
	int num = 0, highest = 0, lowest = 0;

	do
	{
		cout << "Enter a number or -99 to exit: ";
		cin >> num;
		
		if (num > highest)
		{
			highest = num;
		}

		if (num < lowest)
		{
			lowest = num;
		}
	
		
	} while (num != -99);
	
	cout << "\nThe highest number is " << highest << endl;
	cout << "The lowest number is " << lowest << endl;

	return 0;
	
	
}
If you want to keep a do while loop, you could pull lines 11 and 12 and put them at line 8 (before the do starts), then repeat them at line 23 (before you evaluate the while condition).

I figured it out thanks for the help

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

#include<iostream>
using namespace std;

int main()
{
	//Declare variables
	int num = 0, highest = 0, lowest = 0;

	cout << "Enter a number or -99 to quit: ";
	cin >> num;

	if (num == -99)
	{
		return 0;
	}

	highest = num;
	lowest = num;

	while (num != -99)
	{
		cout << "Enter a number or -99 to quit: ";
		cin >> num;

		if ( num == -99)
		{
			break;
		}

		if (num > highest)
		{
			highest = num;
		}

		if (num < lowest)
		{
			lowest = num;
		}
	}
	
	cout << "\nThe highest number is " << highest << endl;
	cout << "The lowest number is " << lowest << endl;

	return 0;
Topic archived. No new replies allowed.