Hey Everyone, I'm completly new to C++ and currently am using a book that was reccomended on this site. One of the exercises was to change some coding for a basic math game where you start froma a number you input, and between you and the PC whoever hits 0 first wins. (the numbers you can use are 1 and 2)
#include <iostream>
usingnamespace std;
int main () {
int total, n;
cout << "Welcome to NIM. Pick a starting total: ";
cin >> total;
while (true)
{
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 PWNZOR!!" << endl;
break;
}
cout << "Enter Num to subtract (1 or 2): ";
cin >> n;
while (n < 1 || n > 2) {
cout << "Input must be either 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;
}
The problem is that when you make the game start off with 0, it automaticaly goes into negative causing the game to go into a never ending loop. The exercise wants me to add a line so that if the user inputs the number 0 they will get a message telling them that they need to type in a value higher than 1.
I've been solving all the other exercises with trying different things out, but for some reason this one has me scratching my head. If I could get afew hints, I really want to tackle this down without getting the answer. But I dont want to get frustrated either.
#include <iostream>
usingnamespace std;
int main () {
int total, n;
while(total <= 0) {
cout << "Welcome to NIM. Pick a starting total greater than zero: ";
cin >> total;
}
while (true)