basic math game

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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

using namespace 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.

Thanks for the help!
What about something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main () {
    int total, n;
    
    while(total <= 0) {
        cout << "Welcome to NIM. Pick a starting total greater than zero: ";
        cin >> total;
    }
    while (true) 
          
@JMJAtlanta
What are you doing with the second while ?

while (true)

Sorry if I was unclear. I only posted the top lines of code. The while(true) was a continuation of the original code, line 10 of the OP's code.
Topic archived. No new replies allowed.