Find largest and smallest number

Hi I am new to C++ programming and I have an assignment that has been driving me crazy for the past few days. The assignment is as follows...

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 will display the largest and the smallest numbers entered. After every series of numbers ask the user "Do you wish to continue (Y/N)?". If the answer is 'y' or 'Y' the program should continue asking for series of integers.

I am not sure exactly what I am supposed to do for this to work properly. Any help would be greatly appreciated!
I'm going to spoil it for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#define EXIT -99
#define CONTINUE 1
using namespace std;
int smallest, largest, current;
int main() {
	while (true) {
		while (true) {
			cout << "Number: ";
			cin >> current;
			if (current == EXIT) break;
			smallest <?= current;
			largest >?= current;
		}
		cout << "Smallest: " << smallest << endl;
		cout << "Largest: " << largest << endl;
		cout << "1 to continue, anything else to bugger off" << endl;
		cin >> current;
		if (current != CONTINUE) break;
	}
	return(0);
}


Also, a nested while() loop is not the greatest idea, but most people frown upon using goto statements. However, coming from a background in assembly language, I can safely say that goto will not kill your cat, or make your programs any worse.

This program would be more optimised, but you'll probably get caned by your teacher:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#define EXIT -99
#define CONTINUE 1
using namespace std;
int smallest, largest, current;
int main() {
	while (true) {
	loop:
		cout << "Number: ";
		cin >> current;
		if (current == EXIT) break;
		smallest <?= current;
		largest >?= current;
	}
	cout << "Smallest: " << smallest << endl;
	cout << "Largest: " << largest << endl;
	cout << "1 to continue, anything else to bugger off" << endl;
	cin >> current;
	if (current == CONTINUE) goto loop;
	return(0);
}
Last edited on
Topic archived. No new replies allowed.