# Need help with a loop program

• 10-08-2006
hieugene
Need help with a loop program
hey,
im writing a program that requires me to have the user type in random numbers between 0 and 9999, and my program should keep track of all the numbers. when the user types -1, the program should show the maximum and minimum number they entered. how do i keep track of all the numbers the user enters and how do i show the maximum and minimum number of those set of numbers? (it's supposed to be a loop program)
thanks!
• 10-08-2006
bumfluff
I take it you know how to loop? Why not use a vector of integers and each time push back untill they type -1.
• 10-08-2006
Queatrix
Vectors have no use for this task.
You only need to keep track of 2 number.
Code:

```int max = 0; int min = 0; void AddNumberFromUser(int num) {   if(num > 0)   {       if(num > max)         max = num;       if(num < min)         min = num;   }   else   {       cout << "The largest user input is " << max << ", and the smallest is" << min << ".";   } }```
• 10-09-2006
JukeBoxHero
Code:

```#include <iostream> #include <vector> using namespace std; int main() {         int number,max=0,min=9999;         vector<int> randomNumbers;         cout << "Type in random numbers 0 - 9999. Enter -1 to stop.\n";         do{                 cout << "Number: ";                 cin  >> number;                 if(number>=0)                 {                         randomNumbers.push_back(number);                         if(number<min)                                 min=number;                         if(number>max)                                 max=number;                 }         }while(number>=0);         if(randomNumbers.size()>0)         {                 cout << "\nYou entered the following numbers:\n";                 for(int i=0;i<randomNumbers.size();i++)                         cout << randomNumbers[i] << endl;                 cout << "\nThe min was " << min << " and the max was " << max << ".\n";         }         else         {                 cout << "You didn't enter any numbers.\n";         }         return 0; }```
this will ask them to enter numbers until then enter a negative number, then display all the numbers they picked, the min, and the max. If you don't want to list all the numbers they picked then you don't need the vector or the for loop(or the cout right before the for loop).
• 10-09-2006
hieugene
thanks guys but your methods are advanced for me. I'm very new to programming and hardly know what you guys are talkin about. Anyway, i made a code for the program i'm making, but whenever i run the program and it finds the min., the min. is -1. It finds the max correctly, but the min is always -1. Can someone help me?
• 10-09-2006
hieugene
Sorry guys

Code:

```#include <iostream> using namespace std; int main() {         char choice;         double number=0, min=number, max=number;         do {                 cout << "Enter as many numbers as you want. Enter -1 to stop." << endl;                 do {                 cout << "Enter a number: ";                 cin >> number;                 if ( number < -1 || number > 9999 ) {                         cout << "Only enter numbers greater than -1 and less than 9999" << endl;                 }                 else if ( number > max ) {                 max = number;                 }                 else if ( number < min ) {                 min = number;                 }                 } while (number != -1);                 cout << "Min = " << min << endl;                 cout << "Max = " << max << endl;                 cout << "Continue? (y or n)";                 cin >> choice;         } while ( choice == 'y');                         if (choice == 'n') {                 cout << "End program." << endl;                 } }```
• 10-09-2006
anon
So you are using Queatix's idea? His code has one very obvious bug and you are making the same mistake. It's min you are having trouble with. Can you spot the error?

If you fix that, min would still print -1. You should not be processing the last input.
• 10-09-2006
hieugene
can someone just tell me the answer? a dry answer....im so confused with what to do to fix this program.
• 10-09-2006
JukeBoxHero
Quote:

Originally Posted by hieugene
Sorry guys

Code:

```#include <iostream> using namespace std; int main() {         char choice;         double number=0, min=number, max=number;         do {                 cout << "Enter as many numbers as you want. Enter -1 to stop." << endl;                 do {                 cout << "Enter a number: ";                 cin >> number;                 if ( number < -1 || number > 9999 ) {                         cout << "Only enter numbers greater than -1 and less than 9999" << endl;                 }                 else if ( number > max ) {                 max = number;                 }                 else if ( number < min ) {                 min = number;                 }                 } while (number != -1);                 cout << "Min = " << min << endl;                 cout << "Max = " << max << endl;                 cout << "Continue? (y or n)";                 cin >> choice;         } while ( choice == 'y');                         if (choice == 'n') {                 cout << "End program." << endl;                 } }```

Code:

`double number, min=9999, max=0;`
don't need to give number a initial value, thats what the cin is for(though you can if you want too). The min must be set as 9999, otherwise it will always stay at 0.

Code:

```else if ( number > max ) {     max = number; } else if ( number < min ) {     min = number; }```
don't use the else if's. just do one else and check for the max and min under it. You need to check for both because if the user only enters 1 number, then it would have only affected the max, skipping right over the min.

Code:

```else {           if(number<min)           {                     min=number;           }           if(number>max)           {                     max=number;           } }```
Code:

```if ( number < -1 || number > 9999 ) {           cout << "Only enter numbers greater than -1 and less than 9999" << endl; }```
should be number < 0 so that when the user enters -1 it doesn't change the min. Also, if you don't want the message about only entering number greater than -1 and less than 9999 to come up when -1 is entered, put in another if else.

Code:

```if ( number < 0 || number > 9999 )          {                    if(number=-1)           {                     break;           }           else           {                     cout << "Only enter numbers greater than -1 and less than 9999" << endl;           } }```
• 10-09-2006
h_howee
Quote:

how do i keep track of all the numbers the user enters
including the numbers under 0 and over 9999?

Code:

```#include <iostream> #include <vector> using namespace std; void A(); int num = 0; int min = 0; int max = 0; vector<int> a; int main() {     for (;;)     {         cin>>num;         A();     }     return 0; } void A() {     if (num == -1)     {         cout<<"min: "<<min             <<endl             <<"max: "<<max;         return;     }     if (num < 0)         return;     if (num > 9999)         return;     if (num < min)         min = num;     if (num > max)         max = num;     a.push_back(num); }```
havent tested it tho
• 10-10-2006
iMalc
What makes you think that you need to keep track of all numbers entered?
• 10-10-2006
h_howee
Quote:

Originally Posted by iMalc
What makes you think that you need to keep track of all numbers entered?

u talking to me, hiugene or who?
• 10-11-2006
jafet
You. Drop the vector. All you need is a max and min.

Ok, step by step...

Code:

```set max to the first input number, or to a very low number (-1 is low and invalid so we can use it) user inputs 10 we know that no matter whatever else the user inputs the max is at least 10, so max is set to 10 user inputs 8 max cannot be 8 (because there is a 10), so max is unchanged user inputs 17 set max to 17 user inputs 16 leave max unchanged user inputs -1 done, the max is 17```
For min, just invert the comparisons. Unless you want to set a lower and/or upper limit on user input, you should set max and min to the first input number.