# Thread: Saving the greatest/ least value...

1. ## Saving the greatest/ least value...

Okay,

I have to create a menu ( which I did) and have 3 options. (done) In option A I have to allow the user to enter how many numbers he wants to input (a limit). Then after that limit of numbers is input, the user is told which number was the largest. This is the part that I'm having trouble with. All it prints for the value is what the last number that was entered was.

I'm getting the same error in section B. All it wants to display for the lowest number entered is -99... I think I have an idea for that one, but section A still gives me problems.

I have to use loops.. do..while, for, while, if / else. Any pointers as to what I'm missing here?

Code:
```// Week 5 - Control Structures II- Philip McCrary - 11-08-06

#include<iostream>

using namespace std;

int number;
int limit, counter;
int temp1, temp2;
const int SENTINEL = -99;

int main()
{ //open main

{ // open while 1
cout << "Welcome to CS106 Week 5: Control Structures II." << endl;
cout << "Please choose from the following options below." << endl;
cout << "A: What's the largest number?" << endl;
cout << "B: What's the smallest number?" << endl;
cout << "C: Flee to Windows" << endl;
cout << "Please choose now: ";
cout << endl;

if ((menuItem >= 'A' || 'a') && (menuItem <= 'C' || 'c'))
{ // open if main
{ // open switch 1
case 'a':
case 'A':

cout << "This section will find the largest number? " << endl;
cout << "How many numbers do you want to enter? ";
cin >> limit;
cout << endl;
cout << "Enter " << limit << " numbers: " << endl;

counter = 0;

for (counter = 0; counter < limit; counter++)
cin >> number;

temp1 = 0;
if (number > temp1)
temp1 = number;
if (temp1 != 0)
cout << "\nYour largest number out of " << limit << " was " << temp1 << "." << endl;
cout << endl;

break;

case 'b':
case 'B':

cout << " Enter a group of numbers. I will tell you the smallest number. \n Enter " << SENTINEL << " to exit." << endl;
cin >> number;

counter = 0;
temp2 = 0;

while (number != SENTINEL)
{ // open while 3
counter++;
cin >> number;
}
if (number < temp2)
temp2 = number;

cout << "Your smallest number out of " << counter << " was " << temp2 << "." << endl;
cout << endl;
break;
case 'c':
case 'C':
cout << " Goodbye. " << endl;
return 0;
} //close switch 1
} // close if main
else
{
cout << "Please enter a valid menu option; A, B, or C." << endl;
cout << endl;
} // end else

} // close while 1
return 0;
} // close main```

2. 1. Do not use goto, simply break out of your while loop.
2. You do not need to set counter twice:
Code:
```counter = 0;
for (counter = 0; counter < limit; counter++)```
Use this:
Code:
`for(counter = 0; counter < limit; counter++)`
3. Number is overwriten every time you cin. Thus the last number entered is the greatest. Create an array of size limit and use that.
Code:
```cin >> limit;
cout << endl;
cout << "Enter " << limit << " numbers: " << endl;
int numbers[limit];
for(counter = 0; counter < limit; counter++)
cin >> numbers[counter];```
EDIT: This:
Code:
```if (number < temp2)
temp2 = number;```
needs to be in the while loop to be counted. Also, this will keep looping until you enter -99 (SENTINEL), as it says
Code:
`while(number != SENTINEL)`
Unless you enter -99, this will keep looping. You neeed a way to know how to exit.

3. See that's my problem. We haven't covered arrays yet, so to include it would be just wrong.

Is there any way of doing this in a while, for, or do.. while loop.... or even an if / else statement?

Thanks for any help guys!

4. I don't think so. You would overwrite the previous value Try the non-array fixes I suggested and see if that helps. If not, ask your teacher if you may use arrays. Hey, they might even be impressed that you are ahead (assuming you use them correctly).

5. There in lies the apparent problem though... I don't know what an array is.

Shouldn't my if statement save only the highest value?

Code:
```if (number > temp1)
temp1 = number;```

6. I'll take your second question first: No, as right before that you set temp1 = to 0. Also, this does not loop. You need code like:
Code:
```for (counter = 0; counter < limit; counter++)
{
cin >> number;
if (number > temp1)
temp1 = number;
}```
This should work. As for your first question, check out the tutorials.

7. DUDE!!!

I am so dancing a jig in my living room right now! the brackets in the for statement worked!!!
You are so awesome. I can't believe I didn't think of that!
*Super-long-distance HIGH FIVE*

Now I just have to figure out why section B returns only a 0 answer.

8. Almost the same thing:
Code:
```cout << " Enter a group of numbers. I will tell you the smallest number. \n Enter " << SENTINEL << " to exit." << endl;
counter = 0;
temp2 = 0;

while (number != SENTINEL)
{ // open while 3
counter++;
cin >> number;
if (number < temp2)
temp2 = number;
}```

9. Okay. Here is the updated code.

Section B returned -99 as the smallest value, so I added the != Sentinel in the if statement and now it returns 0 again.

You've been a BIG help man. More so than the appointed programming tutor at the college... he's too hard to get a hold of!
Thanks.. really I mean it.

10. Sure thing Good luck! One last question: does B work? Will it show your smallest number?

11. No. With the snippet of code you showed me, it returns -99 as the smallest number. I inserted a && != SENTINEL in the if staement and now it only returns a ) value.

12. hmm...why not try the for method:
Code:
```int limit2 = 0;
case 'B':
int limit2 = 0;
cout << "This section will show the smallest number."
cout << "How many numbers do you want to enter? ";
cin >> limit2;
cout << endl << "Enter " << limit2 << " numbers: " << endl;

for (counter = 0; counter < limit2; counter++)
{
cin >> number;
if (number < temp2)
temp2 = number;
}```
I know this does not probably do exactly what you want, but does it work?

13. Section B requires a sentinel to flag the end of user input. The user has to be able to enter any amount of number and end with -99. Then it should display the proper results.

14. I see part of the problem.. I have temp2 = 0, but then i ask it to replace the value of temp 2 with number if number is less than 0. I'm trying to rewrite that part now.

15. I can't figure it out If you wait until tomorrow the forum gods (Salem, quzah, Prelude, etc) will answer your question. Sorry I can't be of more help!