The first problem I see is that you didn't initialize a couple of variables.
You should either initialize both minValue
and maxValue
variables with something which will overwritten in every case in the first loop (typically "positive/negative infinity", as provided by <limits>
), or just set both to Number
in the first iteration, regardless of their current value. So I'd suggest to fix this by replacing
if(maxValue <= Number)
maxValue = Number;
if(Number <= minValue)
minValue = Number;
with
if(maxValue <= Number || ct == 1)
maxValue = Number;
if(Number <= minValue || ct == 1)
minValue = Number;
as ct == 1
will be true in the first iteration.
That said, you check the 0..20
range condition on the wrong variable. You check it on the Number
variable, but you should check the numCount
variable. But you also didn't respect the requirement that the variable to store the "number of numbers" should be Number
, so you did check the correct variable, but used the wrong to read the input into. This should fix this issue (I changed the variable name in the cin >>...
line + moved the check outside your main loop):
cout << "How many numbers would you like to enter? ";
cin >> Number;
if(Number > 20|| Number < 0)
{
for(int errorCt = 1; errorCt <= 4; errorCt += 1)
...
if(errorCt == 4)
{
cout << "You have had 3 attempts to enter a valid" <<
"number. \nPlease try this program again when you" <<
"are able to follow directions.";
cout <<"\nLBn\n"<<"L4P2LB.cpp\n"<<"11-05-12\n";
return 0;
}
cout << Number << "is not within range.\n" <<
"Please enter a number from 0 to 20: ";
cin >> Number;
} //end for loop
}
for(ct = 1; ct <= Number; ct += 1)
{
...
}
...