How to find largest number with sentiel-controlled loop

Hi, this is my first time posting here. I'm in my first semester of c++ class. My HW assignment says:

Have user enter positive integers and keep track of the largest one. User can type -99 to quit.

Ex: "Enter a positive integer (-99 to quit)"
...user enters a positive number
"Enter another positive integer (-99 to quit)"
...user enters another one and so on until entering -99
"The largest number you entered is: "

I'm having trouble grasping how to make use of the sentinel value.

Here's the code I tried to make which I'm pretty sure is wrong... actually I am sure it's wrong lol.

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

using namespace std;

int main()
{
	int x = 0;
	int y = 0;
        
        cout << "Enter a positive integer (-99 to quit): " << x << endl;
        cin >> x;
        cout << "Enter another positive integer (-99 to quit): " << x << endl;
        cin >> x;

	while (int != -99; int i = 0; i<100; i++)
	{
		cin >> x;
		if (x>y)
			y = x;
	}

	cout << "The biggest number is: " << y << endl;

	return 0;
}
Last edited on
I tried this code too. It works better but still isn't right

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
	int x = 0;
	int y = 0;

	cout << "Enter a positive integer (-99 to quit): " << x << endl;
	cin >> x;
	cout << "Enter another positive integer (-99 to quit): " << y << endl;
	cin >> y;

	while (x != -99, x = 0, x < 100, x++);
	{
		cin >> x;
		if (x>y)
			y = x;
	}
	cout << "The biggest number is: " << y << endl;

	return 0;
}
I found the answer incase one day someone needs it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
	int x = 0;
	int y = 0;

	cout << "Enter a positive integer (-99 to quit): ";
	cin >> x;

	while (x != -99)
	{
		if (x>y)
			y = x;
		
		cout << "Enter another positive integer (-99 to quit): ";
		cin >> x;
	}
	cout << "The biggest number is: " << y << endl;

	return 0;
}
Topic archived. No new replies allowed.