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.
#include <iostream>
usingnamespace 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
elseif (number >= maxNumber)
{
maxNumber = number;
}
}
//Outputs minimum and maximum values
cout << "Minimum Value: " << minNumber << endl;
cout << "Maximum Value: " << maxNumber << endl;
return 0;
}