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?
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?
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.
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.
Getting two errorsCode:#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; }
--------------------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)
Last edited by Sembhi; 12-06-2007 at 07:49 PM.
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?
wrong syntax for a for statement.for (temp = num)
{
if (num > temp)
largest = temp;
}
ex:
Code:for(int a=0;a<=num;a++) { }
^ Thanks.
Why is largest not displaying?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; }
Most probably because num does not equal zero ?
Then why does the total display?
Why are you using temp as you iterator and also compare it to num?
Last edited by clegs; 12-06-2007 at 10:16 PM.
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.
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?
You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.