The problem with my program is that it is supposed to not accept negative integers. Though it says it will not accept them, the negative integers are still set as the min.
Also, a minor problem with the program not accepting the next input after you enter a negative int.
#include <iostream>
usingnamespace std;
int main()
{
int max, last_num;
//Sets min to infinity so input will always be less
double min = std::numeric_limits<double>::infinity();
cout << "Please enter a list of positive integers in any order at any length.\n";
cout << "Once you are done, please enter a 0 to let me know you are done.\n";
cout << "After that, I will tell you the largest and smallest integers you have entered.\n";
last_num = 1;
max = 0;
do
{
//Takes the input
cin >> last_num;
//Changes max and min over time
if (last_num > max){
max = last_num;}
if (last_num < min && last_num != 0){ // And positive
min = last_num;
}
//Echos input
if (last_num > 0)
{
cout << "You have entered " << last_num << ".\n";
cout << "max " << max << " and min is " << min << ".\n";
}
//Error message
if (last_num < 0)
{
cout << "I cannot accept negative integers.\n";
cout << "The next number you enter shall not be counted.\n";
//CIN AGAIN IF THEY GIVE YOU A NEGATIVE
cin >> last_num;
}
}
//Terms and Conditions
while(last_num > 0 || last_num != 0);
//End command
if (last_num == 0)
{
cout << "You have finished entering your integers.\n";
cout << "The largest number is " << max << ", and the smallest number is " << min << ".\n";
}
system("pause");
return 0;
}
.
Do ... while is executed before you check the condition. Suppose I enter -5 an line 18 as the first number. On line 21 is not set as max since it is less then max, then on line 24 you set is as min. You don't ho inside the condition at 29 (no printing).
Here are my suggestions:
- after line 18 test if last_num<0, don't ask for a new last_num
- all other cases should be handled by "else" statements
- while(last_num > 0 || last_num != 0) can be written as while(last_num > =0 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
do
{
//Takes the input
cin >> last_num;
//Error message
if (last_num < 0)
{
cout << "I cannot accept negative integers.\n";
cout << "The next number you enter shall not be counted.\n";
}
elseif(lastnum>0)
{
// .......................... handle here you mun and max
}
}while(last_num > =0 )
Though it says it will not accept them, the negative integers are still set as the min.
You need to move the section of code that checks for numbers less than zero to right after the user enters the number. Instead of re-asking for the input you could use the continue statement to return to the beginning of the loop. Because with the if statement if your user enters a second negative number you'll still have a problem.
Also, a minor problem with the program not accepting the next input after you enter a negative int.
The reason it appears that it is not accepting your input is also being caused by the placement of the section where you check for values lower than zero. Right now you are checking for that value right before the end of the loop, so after you re-enter the number you loop back to the beginning of the loop, where it is asking for another entry.
There is no need for the numeric_limits call since zero will work because you are insuring all your numbers will be greater than zero.
Also why are you defining min as a double?
There is also no need for that if statement after your do/while loop. The last_number will always be equal to zero if you get out of your loop.