Need help with programming

Hello Guys, I am trying to write a code that lets me input an infinite amount of numbers and save the top 2 largest numbers and the 2 lowest numbers with a minimum user input of 2 times before they are able to use the sentinel number to break out of the program. I can only use basic loops, so no arrays and strings.

This is what I got so far:

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

using namespace std;

int main()
{
int number;
int maxnum=0;
//int num=0;
//int smallnum=0;
//int smallestnum=0;

while(number !=-1){

cout<<"Please type a number between 0 and 10,000"<<endl;
cin>>number;

if (number<=-2 || number>=10000){

cout<<"Invalid entry."<<endl;}

if(maxnum>=-2&&maxnum<=10000){
maxnum=number;}

cout<<"largest number is:"<<""<<maxnum<<endl;

if(number==-1){

cout<<"you have terminated the program, goodbye."<<endl;}


The problem that I am having right now is that when I type in a number greater than 10,000 my code will say "invalid number" but when I terminate the program and tell it to print out the max number it prints out the highest number typed regardless of the condition that it has to be less than 10,000. So for example, if I type 10, 888 this would be the max number printed. Any thoughts?? Put the code you need help with here.
Last edited on
Hello codemonkey03,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Your program flow is all backwards. The first thing you should be checking for is if number == -1. The use of an if/else would work. Then in the else block check if the number is in the proper range. I would suggest if (number < 0 || number > 9999) or you could use ">-10000" your choice. Since you have checked for "-1" anything < 0 is out of range.

The If statement for finding the maximum number is not all there because you need to two maximum numbers and you have nothing for the minimum numbers.

The while loop need to be forced to loop at lest four times before it can end. Thinking of an || or && condition to work from a counter.

Hope that helps,

Andy
Please only post once. The exact same people will see your question regardless of which area you put it in.

you appear to have had a good idea and tossed it.
4 variables, the 2 biggest and 2 smallest.
then you need some tests and shuffles. If its bigger than the biggest, the new value is biggest and the old biggest is 2nd biggest. Similar for smallest. If its bigger than the 2nd biggest (and less than biggest) just replace 2nd biggest, similar for smallest. Got to do all that on each value. I am assuming that when the user enters the first number, you set all 4 values to that value. I am also assuming that the biggest and smallest can be the same entry (?), for example if a stubborn user put in 5,5,5,5,5,5, ...
Registered users can post here. Sign in or register to post.