# Thread: Saving the largest and smallest numbers.

1. ## Saving the largest and smallest numbers.

I have a program where the user has to input numbers over and over, if they want to end the program they hit 0. After they hit zero though I have to display the largest and smallest numbers they entered. Tips on how I can do this?

2. I find it hard to believe you can't come up with a way to do this without someone giving you an algorithm. How about you try to come up with a way to do this first? If it seems reasonable, and it makes sense, then try coding it.

3. What I had in mind was using if, setting the first number entered to a variable called temporary, and have all the numbers entered compared to that, if the number entered bigger/smaller have that number become temporary. Do you think that will work?

Yea I'm confused. This is my code.

Code:
```#include <iostream.h>

int main()
{
int sum = 0;
int num;
int total = 0;
int temp = 0;
int largest;

do
{
cout << "Enter a number (Enter 0 to quit): ";
cin >> num;

for (temp = num)
{
if (num > temp)
largest = temp;
}

total = total + num;

if (num == 0)

{
cout << largest;
cout << total << " ";
break;
}

}
while (1);
return 0;
}```
Getting two errors

--------------------Configuration: 6.1 - Win32 Debug--------------------
Compiling...
6.1.cpp
D:\P2\C++\6.1.cpp(27) : error C2143: syntax error : missing ';' before ')'
D:\P2\C++\6.1.cpp(27) : error C2143: syntax error : missing ';' before ')'
Error executing cl.exe.

6.1.obj - 2 error(s), 0 warning(s)

4. Originally Posted by Sembhi
What I had in mind was using if, setting the first number entered to a variable called temporary, and have all the numbers entered compared to that, if the number entered bigger/smaller have that number become temporary. Do you think that will work?
Well if it's going to output BOTH the smallest and biggest, you'll probably need 2 temp variables. And yeah that method could work.

5. Yea I know, I'm just trying to get one to work first.

Can someone take a look at the code posted above and tell me whats wrong with it?

6. for (temp = num)
{
if (num > temp)
largest = temp;
}
wrong syntax for a for statement.
ex:

Code:
```for(int a=0;a<=num;a++)
{
}```

7. ^ Thanks.

Code:
```#include <iostream.h>

int main()
{
int sum = 0;
int num;
int total = 0;
int temp = 0;
int largest = 0;

do
{
cout << "Enter a number (Enter 0 to quit): ";
cin >> num;

for(int temp=0;temp<=num;temp++)

{
if (num > temp)
largest = temp;
}

total = total + num;

if (num == 0)

{
cout << largest;
cout << total << " ";
break;
}

}
while (1);
return 0;
}```
Why is largest not displaying?

8. Most probably because num does not equal zero ?

9. Then why does the total display?

10. Why are you using temp as you iterator and also compare it to num?

11. clegs is correct. Your for loop is quite flawed and not necessary.
Let me ask you: what is your code supposed to do? What is the "algorithm" you have in mind to do this? It's a very simple project, but your code is all wrong.
Can you clarify how you're thinking you can do this? If you don't have an idea on the flow of your application, you need to think of one. As I like to say, re-think your design! That is, if your current design you have in mind is not correct.

12. Yeah, the inner loop is completely unnecessary.

Can you try to describe, in words, what you're trying to do? Assume you have a variable named 'largest' that holds the largest number previously found, and assume you have a variable called 'num' which holds the most recent number. How do you decide whether to update 'largest' or not?