Another question.

I got my previous question figured out and now I am on a different example in the eBook I am learning from. I need to make a program repeat after the game has finished. I have made the NIM game where you choose a total and then choose to subtract 1 or 2 from the total until the total is 0.

How would I make that program repeat? I have looked at others code and I cannot figure out how to make it repeat.

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
#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
     int total, n;

     cout << "Welcome to NIM. Pick a starting total: ";
     cin >> total;

     while (total < 1) {
         cout << "Input must be greater than 1" << endl;
         cout << "Re-enter: ";
         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 win!" << endl;
             break;
         }

         cout << "Enter num to subtract (1 or 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;
         }
     }

     return 0;
}
Last edited on
Howdy Tribal,

If you want that code to repeat, then add another while loop above while(total>1). You can prompt the user for another round or just do while(true) to make it play until the user clicks the red X. =)
Ok I got the program to repeat by adding a while (true) loop but I don't know how to prompt the user. I tried adding:

1
2
3
4
while (true) {
       cout << "Would you like to play again (Y or N): ";
       cin >> again;
}


but after the game completes it goes into an endless loop of "Re-enter: Input must be greater than 1" Do you know how to fix that?
The loop needs to have the entire game in its body. Take that parenthesis and move it down to the bottom, just above your return 0; statement. Those loops will execute all commands within it's body and nothing else.

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
50
#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
     int total, n;

     while (true){ //While declaration here

     cout << "Welcome to NIM. Pick a starting total: ";
     cin >> total;

     while (total < 1) {
         cout << "Input must be greater than 1" << endl;
         cout << "Re-enter: ";
         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 win!" << endl;
             break;
         }

         cout << "Enter num to subtract (1 or 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;
         }
     }
     }              /* Put closing bracket here. You want all instructions above this point to repeat.*/
     return 0;
}


Remember that while (true) will loop forever. There is nothing in its parenthesis that will eventually equal false.

You need to put a condition inside the while loop that will be false at some point.

What you could do is this (assuming again is a char type):

 
while(again != 'N')


This will cause the loop to repeat until the user types 'N'

Or you could do this:

 
while(again == 'Y')


This will cause the loop to repeat as long as the user types 'Y' and nothing else.

The only other thing to note is that you should put your question about playing again down at the bottom, just above the closing bracket. The reason is because once the end of the loop is reached, control will return to the top of the loop, and the condition will be tested again.
Last edited on
Topic archived. No new replies allowed.