### Thread: Initial integer > 0

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
3
Rep Power
0

#### Initial integer > 0

Hey all; I am a beginner below is the code, I am having difficulty revising so program only accepts greator than 0. I have tried a couple of code imputs but so far no good. I am sure this would be easy for most of you and I would really appreciate your assistance.

#include <iostream>
using namespace std;

int main() {
int total, n;

cout << "Welcome to NIM. Pick a starting total: ";
cin >> total;
while (true) {

// Pick best response and pick results.

if ((total % 3) == 2) {
total = total - 2;
cout << "I am subtracting 2." << endl;
} else {
total--;
cout << "I am subtracting 1." << endl;
}
cout << "New Total is " << total << endl;
if (total == 0) {
cout << "I win!" << endl;
break;
}

// Get user's response; must be 1 or 2.

cout << "Enter num to subtract (1 ro 2): ";
cin >> n;
while (n < 1 || n > 2) {
cout << "Input must be 1 or 2." << endl;
cout << "Re-enter: ";
cin >> n;
}

total = total - n;
cout << "New total is " << total << endl;
if (total == 0) {
cout << "You win!" << endl;
break;
}
}
system("PAUSE");
return 0;
}
2. Given the spelling, this program looks like your work. You seem to be asking a question you already solved:
Code:
```// Get user's response; must be 1 or 2.

cout << "Enter num to subtract (1 ro 2): ";
cin >> n;
while (n < 1 || n > 2) {
cout << "Input must be 1 or 2." << endl;
cout << "Re-enter: ";
cin >> n;
}```

For clarity, this is the problem you're worried about?
cin >> total;
total could be a silly value.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
3
Rep Power
0
Maybe I should have been more clear if you start with a negative, the program will continue forever, so what I would need to do is add code so that, if the user imputs a negative number it would either not allow or redirect to a positive. :)
4. "in" and "out" are antonyms; they have opposite meanings.

"input" and "output".

Since the value of n is unimportant, this is a game, after all, and if someone tries to fool a game it just doesn't matter, I'd use absolute value.

http://mathworld.wolfram.com/AbsoluteValue.html

Perhaps I wasn't clear. You've already demonstrated the ability to test values and try again until an acceptable value comes along.

Use a similar technique when you get the total.

cin >> total;