Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
March 21st, 2013, 09:18 AM
 brofla285
Registered User

Join Date: Mar 2013
Posts: 3
Time spent in forums: 29 m 2 sec
Reputation 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
March 21st, 2013, 12:51 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,148
Time spent in forums: 1 Month 3 Weeks 2 Days 7 h 54 m 32 sec
Reputation Power: 455
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.
__________________
[code]Code tags[/code] are essential for python code!

#3
March 25th, 2013, 11:25 AM
 brofla285
Registered User

Join Date: Mar 2013
Posts: 3
Time spent in forums: 29 m 2 sec
Reputation 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
March 25th, 2013, 12:37 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,148
Time spent in forums: 1 Month 3 Weeks 2 Days 7 h 54 m 32 sec
Reputation Power: 455
"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;
mythos_ agrees!

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Initial integer > 0