Issue with Sentinel Value being considered in the series of numbers

Hello, I have an assignment for my CS 1336 class. The question for the assignment is in the first comments in the following program. My problem is after executing the following code, the sentinel value that terminates the code is considered as one of the numbers in the series input by the user. The program always gets the maxNumber correct, but the minNumber is always -99 (the sentinel). How can I fix this? Any suggestions? Thank you in advance!

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
49
50
51
52
53
54
55
56
57
58
59
60
  // Assignment 3.cpp 
//Write a program with a loop that lets the user enter a series of integers, followed by 99 to
//signal the end of the series. After all the numbers have been entered, the program should
//display the largest and smallest numbers entered.
//

#include "stdafx.h"

#include <iostream>
using namespace std;

int	main()
{
	//Declaring variables used
	int number, minNumber, maxNumber, counter;
	//Declaring the sentinel value that terminates the program
	const int SENTINEL = -99;

	counter = 0;

	//User inputs a series of integers
	cout << "Enter a series of integers (-99 to quit):\n";
	cin >> number;

	//While-loop that checks the input from the user
	while ( number != SENTINEL )
	{
		//When the numbers entered are not equivalent to the SENTINEL value,
		//the loop assigns the values as either a max or min number and
		//adds +1 to the counter each time it runs through
		cin >> number;
		counter++;
		if ( maxNumber < number )
			maxNumber = number;
		else if ( minNumber > number )
			minNumber = number;
	} 
	
	//If the user initially enters -99 as their value, the counter will be equal to 0,
	//and the following code will execute
	if ( counter == 0 )
	{
		cout << "No data entered." << endl;
	}
	//After entering the series of numbers, then the SENTINEL value, 
	//the program will display the following.
	else
	{
		cout << "The largest number was: " << maxNumber << endl;
		cout << "The smallest number was: " << minNumber << endl;
		cout << "The amount of numbers entered was: " << counter << endl;
	}


	cout << endl <<endl;
	system("pause");
	return 0;

}
Don't do the comparisons on lines 33-36 if the number input is the sentinel. I'd just move the cin>>number into the condition of the while loop, like so:
while(std::cin>>number && number != SENTINEL) {
I changed my program a little bit but now my issue is that the minNumber is always 0, yet the maxNumber actually changes correctly.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Assignment 3.cpp 
//Write a program with a loop that lets the user enter a series of integers, followed by 99 to
//signal the end of the series. After all the numbers have been entered, the program should
//display the largest and smallest numbers entered.
//

#include "stdafx.h"

#include <iostream>
using namespace std;

int	main()
{
	//Declaring variables used
	int number, minNumber, maxNumber, counter;
	//Declaring the sentinel value that terminates the program
	const int SENTINEL = -99;
	number = 0;
	minNumber = number;
	maxNumber = number;
	counter = 0;

	//User inputs a series of integers
	cout << "Enter a series of integers (-99 to quit):\n";
	cin >> number;

	//While-loop that checks the input from the user
	while ( number != SENTINEL )
	{
		//When the numbers entered are not equivalent to the SENTINEL value,
		//the loop assigns the values as either a max or min number and
		//adds +1 to the counter each time it runs through
		counter++;
		if ( number > maxNumber )
			maxNumber = number;
		if ( number < maxNumber && number <= minNumber )
			minNumber = number;
		cin >> number;
	} 
	
	//If the user initially enters -99 as their value, the counter will be equal to 0,
	//and the following code will execute
	if ( counter == 0 )
	{
		cout << "No data entered." << endl;
	}
	//After entering the series of numbers, then the SENTINEL value, 
	//the program will display the following.
	else
	{
		cout << "The largest number was: " << maxNumber << endl;
		cout << "The smallest number was: " << minNumber << endl;
		cout << "The amount of numbers entered was: " << counter << endl;
	}


	cout << endl <<endl;
	system("pause");
	return 0;

}
You set minnumber = 0 at the start of the program, so only numbers less than that will affect the value of minnumber. You should set starting value of minnumber to be the maximum possible number.
Thank you @Mats and @Zhuge for replying! I tried both of your advice but then kind of just rewrote my program. If anyone is interested in seeing it, here it is.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Assignment 3.cpp 
//Write a program with a loop that lets the user enter a series of integers, followed by 99 to
//signal the end of the series. After all the numbers have been entered, the program should
//display the largest and smallest numbers entered.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int	main()
{
	//Declaring variables used
	int number, minNumber, maxNumber, i;
	//Declaring the sentinel value that terminates the program
	const int SENTINEL = -99;
	
	
	//User inputs a series of integers
	cout << "Enter a series of integers (then enter -99 to quit):\n";
	cin >> number;


	//Checks the input from the user
	if ( number != SENTINEL )
	{
		//if only one number is put into the series, the max and min will be equal
		maxNumber = number;
		minNumber = number;
				 
		//user gets the chance to put in another number into the series,
		//or the user can type -99 (SENTINEL) to terminate program
		cin >> number;

		//if the user decides to terminate the program with -99 (SENTINEL) then
		// the program will display the largest and smallest numbers
		if (number == SENTINEL )
		{
			cout << "The largest number was: " << maxNumber << endl;
			cout << "The smallest number was: " << minNumber << endl;
			cout << endl <<endl;
			system("pause");
			return 0;
		}
	

		//otherwise, if the user decides to enter another number in the series,
		//the following executes
		else
		{
			do{
					//The program determines whether the number is a new maximum and
					//stores the new max into the maxNumber variable, while not equal to -99
					if ( number > maxNumber && number != SENTINEL )
					{
						maxNumber = number;
					}
					//The program determines whether the number is a new minimum and
					//stores the new min into the minNumber variable, while not equal to -99
					if ( number < minNumber && number != SENTINEL )
					{
						minNumber = number;
					}

					cin >> number;
				
				} while (number != SENTINEL );
					
			//After the series of numbers are input by the user, the user will terminate the
			//program with -99 (SENTINEL) and the program will display the largest and
			//smallest numbers of the series
			if ( number == SENTINEL )
			{
				cout << "The largest number was: " << maxNumber << endl;
				cout << "The smallest number was: " << minNumber << endl;
				cout << endl <<endl;
				system("pause");
				return 0;
			}
		}
	}
	

	//If the user inputs -99 (SENTINEL) without inputting other numbers first,
	//the program will display an error message and will ask the user whether 
	// or not they wish to try the program again .
	if ( number == SENTINEL )
	{
		string answer;

		cout << "\n\nError. No data entered." << endl;
		cout << "Try again? ";
		cin >> answer;


		//Basically if the user wishes to try again, the main part of the
		//program is repeated within this if statement
		if ( answer == "Yes" || answer == "yes" || answer == "Y" || answer == "y")
		{
			
			//User inputs a series of integers
			cout << "Enter a series of integers (then enter -99 to quit):\n";
			cin >> number;


			//Checks the input from the user
			if ( number != SENTINEL )
			{
				//if only one number is put into the series, the max and min will be equal
				maxNumber = number;
				minNumber = number;
				 
				//user gets the chance to put in another number into the series,
				//or the user can type -99 (SENTINEL) to terminate program
				cin >> number;

				//if the user decides to terminate the program with -99 (SENTINEL) then
				// the program will display the largest and smallest numbers
				if (number == SENTINEL )
				{
					cout << "The largest number was: " << maxNumber << endl;
					cout << "The smallest number was: " << minNumber << endl;
					cout << endl <<endl;
					system("pause");
					return 0;
				}
	

				//otherwise, if the user decides to enter another number in the series,
				//the following executes
				else
				{
					do{
							//The program determines whether the number is a new maximum and
							//stores the new max into the maxNumber variable, while not equal to -99
							if ( number > maxNumber && number != SENTINEL )
							{
								maxNumber = number;
							}
							//The program determines whether the number is a new minimum and
							//stores the new min into the minNumber variable, while not equal to -99
							if ( number < minNumber && number != SENTINEL )
							{
								minNumber = number;
							}

							cin >> number;
				
						} while (number != SENTINEL );
					
					//After the series of numbers are input by the user, the user will terminate the
					//program with -99 (SENTINEL) and the program will display the largest and
					//smallest numbers of the series
					if ( number == SENTINEL )
					{
						cout << "The largest number was: " << maxNumber << endl;
						cout << "The smallest number was: " << minNumber << endl;
						cout << endl <<endl;
						system("pause");
						return 0;
					}
				}
			
			}
		}
		
		//If the user does not wish to try the program again, then the preogram terminates
		else if ( answer == "No" || answer == "no" || answer == "N" || answer == "n")
		{
			cout << "Okay cool. Thanks for trying."<< endl << endl;
			cout << "PROGRAM TERMINATED.";
			cout << endl <<endl;
			system("pause");
			return 0;
		}

		//If the user does not enter Yes/No or Y/N, without first letter case mattering,
		//then the program terminates
		else
		{
			cout << "Not a good enough answer. Guess you don't want to run the program again.\n Goodbye." << endl << endl;
			cout << "PROGRAM TERMINATED.";
			cout << endl <<endl;
			system("pause");
			return 0;
		}
		

	}

		
	
	cout << endl <<endl;
	system("pause");
	return 0;

}
Whoa, that is a lot of code to do very little. No offense meant.

I think both my note about the while loop and Mats note about your initialization are just two minor changes to your much smaller original program that would have it work.
Topic archived. No new replies allowed.