# Thread: Why does it do this?!?

1. ## Why does it do this?!?

Hello folks, I am a newbie to programming. I will be using C++ over the summer for an independent study course in Computational Physics, so I HAVE to get good at this within the next month. I have been practicing a lot, but I still know little about it, only what I have learned from this website and some terrible books I own.

Anyway, just practicing today, I rewrote a program (which I based off of another simpler one I found in a book). Here it is:

Code:
```#include <iostream>

using namespace std;

int main()
{
int num1, num2, total;
cout << "Enter a positive whole number that is less than or equal to 10: ";
cin >> num1;
if ( num1 > 10 )
{ cout<<"This number is NOT less than or equal to 10\n";
cout << "Please enter a positive whole number that is less than or equal to 10: ";
cin >> num1;
}
if ( num1 < 0 )
{ cout<<"This is NOT a positive number\n";
cout << "Please enter a positive whole number that is less than or equal to 10: ";
cin >> num1;
}
cout << "Now enter another number: ";
cin >> num2;
cin.get();
total = num1 + num2;
cout << "Your numbers total\n" << total << endl;
cin.get();
return 0;
}```
The program works just fine if I type in 4 and 6, but here is the problem. I want someone to enter a number that is less than or equal to 10, so if I enter 11, the program returns the message "This number is NOT less than or equal to 10. Please enter a positive whole number that is less than or equal to 10". This works fine, except that if I enter 11 once again, it just goes on to the next question in the program. So it will not accept a number > 10 the first time, but it will if you enter it a second time. This also happens if you enter a negative number after it has already prompted you to enter a positive number. Why is this?!? Thanks a lot for your time!!!

säki

2. The problem is that that second time around, you've passed the num1 > 10 part and it checks if num1 < 0. This is true, so it accepts it and moves on to adding.

Here's a solution...
Code:
```#include <iostream>

using namespace std;

int main()
{
int num1, num2, total;
cout << "Enter a positive whole number that is less than or equal to 10: ";
cin >> num1;
while (num1 > 10 || num1 < 0)
{
if (num1 > 10)
cout<<"This number is NOT less than or equal to 10\n";
else
cout<<"This is NOT a positive number\n";
cout << "Please enter a positive whole number that is less than or equal to 10: ";
cin >> num1;
}
cout << "Now enter another number: ";
cin >> num2;
cin.get();
total = num1 + num2;
cout << "Your numbers total\n" << total << endl;
cin.get();
return 0;
}```

3. Excellent!!! Thank you very much!!!

säki