Sentinel loop ques

Hey guys!
I recently started programming as a course in college and I am a beginner in programming.I have got an assignment question where I am suppose to write a C++ program that prompts the user to enter a set of numbers ending with -99 and then the program finds and prints the maximum and the minimum of the entered numbers.
The problem is when i enter only -99 it gives me wrong max and min my ques is How do I get an output of "no input" if i enter -99
but get max and min when i input nums including -99 to stop the program.
I know i have to put condition outside the while loop but it dint really work.Also I know i am making a very silly mistake but I am really not able to figure out.

I am at a very basic level of loops here
heres my code

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
  #include <iostream>
using namespace std;
int main()
{
    int num,  max=0, min;

    cout<<"Enter a set of integers ending with -99 to find their maximum and minimum :";
cin>>num;


    while(num != -99)

    {
      cin>>num;

         if(num>max)
          max=num;


          if(num<min)
          min=num;

    }

    if(num==-99)
    cout<<"INVALID";
    else
    cout<<"max"<<max<<endl<<"min"<<min;


  return 0;

    }

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
#include <iostream>
using namespace std;
int main()
{
	int num, max = 0, min = 99; //remember to initialize min.

	cout << "Enter a set of integers ending with -99 to find their maximum and minimum :";
	cin >> num;


	while (num != -99)

	{
		cin >> num;

		if (num>max)
			max = num;


		if (num<min && num != -99)
			min = num; // don't let -99 be assigned to min

	}

	//if (num == -99)
		//cout << "INVALID"; //take these out
	//else
		cout << "max" << max << endl << "min" << min;


		cin.ignore();
		cin.get(); //just to pause the screen
	return 0;

}
hey there!

I tried changing the code it shows the max and min even if i just add input -99
my issue is when i enter -99 i dont want max and min just no input but with other digits i want it to show max and min

My output should be :-
eg1:-Enter a set of integers ending with -99 to find max and min: -99
*NO INPUT*

eg2:-Enter a set of integers ending with -99 to find max and min:
102 346 875 0 -4 8 -99
max : 875
min : -4

if(max>0 || min < 99) cout << "max" << max << endl << "min" << min;

add this to the code...

ok ran into 1 more problem... Here is finished code.
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
#include <iostream>
using namespace std;
int main()
{
	int num, max = 0, min = 99;

	cout << "Enter a set of integers ending with -99 to find their maximum and minimum :";
	cin >> num;


	while (num != -99)

	{
		//cin >> num;

		if (num>max)
			max = num;


		if (num<min && num != -99)
			min = num;

		cin >> num;
	}

	//if (num == -99)
		//cout << "INVALID";
	//else
		if(max>0 || min < 99) cout << "max" << max << endl << "min" << min;


		cin.ignore();
		cin.get();
	return 0;

}
Last edited on
nope it still doesnt shows invalid after i put -99
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
#include <iostream>
using namespace std;
int main()
{
	int num, max = 0, min = 99;

	cout << "Enter a set of integers ending with -99 to find their maximum and minimum :";
	cin >> num;


	while (num != -99)

	{
		//cin >> num;

		if (num>max)
			max = num;


		if (num<min && num != -99)
			min = num;

		cin >> num;
	}

	if (max == 0 && min == 99)
		cout << "INVALID";
	//else
		if(max>0 || min < 99) cout << "max" << max << endl << "min" << min;


		cin.ignore();
		cin.get();
	return 0;

}


Did we get it all this time?
Yess!!
Thanks a ton :)
Topic archived. No new replies allowed.