### 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:
 ``12345678910111213141516171819202122`` ``````#include #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; } 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:
 ``123456789101112131415161718192021`` ``````#include #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; } 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.