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;
}
}
hopefully that will answer all your questions