NEED HELP

Hey Guys this is my question but im having problems with the code it compiles but doesnt run correctly just gives me infinite "Please enter a number".
Tell the user to enter ten numbers (integer) and output the
maximum, minimum and the sum of those numbers, and whether
or not each number is even or odd. You need to use a while loop to
obtain the numbers from the user. You should not declare ten
different variables to store the user input.


#include <iostream>
using namespace std;
int main()
{
int number, max, min, sum;
int x=1;
min=number;
max=number;
while(x<=9)
{
cout<< "Please enter a number"<<endl;
cin>> number;
}
if(number%2==0)
cout<< " number is even" <<endl;

else cout<< "number is odd"<<endl;
{
if(number>max)
{
max=number;
}
if(number<min)
{
min=number;
}
}
x=x+1;

return 0;
}
Last edited on
1
2
3
4
5
while(x<=9)
{
cout<< "Please enter a number"<<endl;
cin>> number;
}


this piece of code will always execute as you've declared x = 1, theres nothing to make it go over 9, hence you will get infinite "Please enter a number" 's

you also wont be able to find the max and min with code you've written. you need to check all of the numbers as they are inputed to find them

also using code tags helps :)
Last edited on
So how would i change this code because right now im only asking for 1 number but i need the code to anticipate 10 numbers that are from negative infinity to infinity. and i also things that the brackets that i have a incorrect.
Well as you need to do it all without storing all the inputs in different variables or an array. Most of your code should be within the while loop.
First you need to ensure that the while loop only runs 9 times, increment x within the while loop not outside it.
Second you need to find whether the input is odd or even and the code your using is fine just use it within the while loop
Third to find the sum keep adding the inputs to sum ...so something like
sum += number
4th to find the max and min you would need to keep comparing the inputs and assign it to max if they are greater kindof like your doing and again you need to do this within the while loop

oh also everything has a limit ;p u cant really go from -infinity to infinity
Last edited on
If you want to execute the process of the user entering an integer value ten times, you are going to want to put every process inside a for-loop that runs ten times.

Inside the for-loop you prompt the user for the value, then perform the operations.

Like Void life said, you need to initialize the min and max in order to be tested against. Ideally, the first user input would go through a slightly different procedure that would automatically set then min and max to its value. You can, however, get away with initializing the min as a very large value and the max as a very small value to ensure the min and max would be reset on the for inputs (ex. int min = large positive number; int max = large negative number)

Comment you code, as it helps you and others understand what is trying to be done.

On one final small note: when a value is incremented by 1, it is virtually always written with the shorthand value++. It makes things easy and more readable.

Good luck!
Topic archived. No new replies allowed.