Problem with Using a Sentinel

I am attempting to write a program that will allow the user to enter as many integers as they want and use a sentinel (-99) to stop entering numbers. The program is then supposed to display the min and max integers.

The program works as it should as long as the user enters values that are both higher and lower than the sentinel. When no values higher than -99 are entered, -99 is displayed as the max, and when no values lower than -99 are entered, -99 is displayed as the min.

I thought that initializing minNumber and maxNumber to the initial number entered by the user would prevent this from happening, but I am clearly missing something. I would appreciate any help you could offer.

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
  #include <iostream>

using namespace std;

int main()
{
   int number,
       maxNumber,
       minNumber;

   //Gets input from the user and establishes a sentinel
   cout << "Enter a series of integers and this program will";
   cout << " display the minimum and maximum integers.\n";
   cout << "Enter -99 when you are done entering integers.\n";
   cin >> number;

   //Initializes min and max values
   minNumber = number;
   maxNumber = number;

   //Loops to get a new integer as long as the end sentinel has not yet been entered
   while (number != -99)
   {
         //Gets a new integer
         cout << "Enter another integer.\n";
         cin >> number;

         //Compares new integer to min value and assigns smallest value to minNumber
         if (number <= minNumber)
         {
                minNumber = number;
         }

         //Compares new integer to max value and assigns largest value to maxNumber
         else if (number >= maxNumber)
         {
                maxNumber = number;
         }
   }

   //Outputs minimum and maximum values
   cout << "Minimum Value:  " << minNumber << endl;
   cout << "Maximum Value:  " << maxNumber << endl;

   return 0;
}
If that sentinel shouldn't be part of your calculation you need a third if before line 29. Only if number != -99 after cin do the calculation.
Thank you! That fixed the problem. I appreciate your help.
Topic archived. No new replies allowed.