Yet another Nim question

Hi,
I am still working through the exercises in C++ Without Fear with a student, & after a break we are back at it. I have not programmed since I took C++ back in 2000, so I am going through the tutorial on this site for myself. The exercises are not assigned per se, but I am pushing the student to do them, but I got stuck on this one myself. For 2.5.3 the books says to "add another loop around the existing one" to make the game "never ending, as long as the human player keeps saying to continue. It seems like simple enough instructions, but we just can't seem to get it to work. The book conveniently did not include a solution code for this exercise, even though there is for nearly every other one in the book.

The program worked fine as the previous exercise, (the NIM game one time through), but I have tried several ways unsuccessfully, and even had a working program from another programmer that worked, but it did not put the whole program inside a new loop, rather it added another while at the bottom.

We got this one (code below) to the point where the only error is parsing error, which I looked up & it says its missing a { or } somewhere. I have gone through more than twice, but I cannot find a missing brace. Can anyone please help me find it, or explain how to do it correctly (differently)? I really want to know both for myself & to explain to the student. Thanks in advance.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
using namespace std;

int main(){

while(true){

    int total;
    int subtract;
    int total_subtract;
    int play_again;

    cout << "Welcome to The Subtraction (NIM) Game." << endl;
    cout << "Please enter a starting total." << endl;
    cin >> total;

    while (total < 1){    //Can also be (total <=0)
        cout << "The starting total must be higher than one." << endl;
        cout << "Please re-input a new starting total and press enter." << endl;
        cin >> total;
    }

    cout << "Please input the highest amount you would like ";
    cout << "to be able to subtract and press enter." << endl;
    cin >> total_subtract;

    while (total_subtract >= total){
        cout << "The highest amount you would like to be able to";
        cout << " subtract must be lower than your starting total." << endl;
        cout << "Please re-input a new amount to subtract and press enter." << endl;
        cin >> total_subtract;
    }

    while (total > 0){    //Can also be (total >= 1) or (true)
        if (total % 3 == 2){
            total = total - 2;
            cout << "I am subtracting 2." << endl;
            cout << "The new total is " << total << "." << endl;
        }else{
            total = total - 1;    //Can also be total--
            cout << "I am subtracting 1." << endl;
            cout << "The new total is " << total << "." << endl;
        }

    //In the book's code they wrote (total == 0), but in the pseudocode they
    //wrote if the total is 0 OR LESS. Therefore I wrote (total <= 0).
        if (total <= 0){    //Can also be (total < 1)
            cout << "I win!" << endl;
            break;
        }

        cout << "Please enter the amount you wish to subtract ";
        cout << "and press ENTER." << endl;
        cin >> subtract;

    //In the book's code they wrote (subtract < 1 || subtract > 2), but in the
    //pseudocode they wrote while input IS NOT 1 or 2. Therefore I wrote:
        while (subtract < 1 || subtract > total_subtract){
            cout << "Please re-enter the amount you wish to subtract ";
            cout << "and press ENTER." << endl;
            cout << "The amount must be between 1 and ";
            cout << total_subtract << " ." << endl;
            cin >> subtract;
        }

        total = total - subtract;
        cout << "The new total is " << total << "." << endl;

    //In the book's code they wrote (total == 0), but in the pseudocode they
    //wrote if the total is 0 OR LESS. Therefore I wrote (total <= 0).
        if (total <= 0){    //Can also be (total < 1)
            cout << "You win!" << endl;
            break;
        }
    cout << "Would you like to play again?" << endl;
    cout << "If yes, type 1. If not, type 0." << endl;
    cin >> play_again;


        if (play_again == 1){
        } else {
        //(play_again == 0){
            break;
        }
    }

    }
}

//system("PAUSE");
return 0;
}
Delete the bracket above where you return 0. Notice how you don't indent your outermost while loop, that might have been where your confusion came from. That should at least let it compile.

But also look at where you place your break statements, it makes part of your code inaccessible. break statements only break out of the inner-most loop of course, so your two break statements (one can never be reached, btw) near the end of the program only break out of the while (total > 0) loop.
Last edited on
Thanks for the helpful reply.We got it to compile, but now we get different results on different compilers:
on my compiler, it runs & I get :
Welcome to The Subtraction (NIM) Game.
Please enter a starting total.
The starting total must be higher than one.
Please re-input a new starting total and press enter.
The starting total must be higher than one.
Please re-input a new starting total and press enter.
The starting total must be higher than one.
Please re-input a new starting total and press enter.
The starting total must be higher than one.
Please re-input a new starting total and press enter.
The starting total must be higher than one.

but on Codeblocks, it runs the game, but every time the game gets to 6 it just asks for a new number. Always at 6:
"i enter 9
i enter 2 as the number to subtract
then it subtracts 1
then i subtract 2
it gets to 6
and it asks me if i want to play again"

This time I see no way to enter the code separately. Would it be better to mark the original post as solved & start another one?
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
using namespace std;

int main(){

while(true){

    int total;
    int subtract;
    int total_subtract;
    int play_again;

    cout << "Welcome to The Subtraction (NIM) Game." << endl;
    cout << "Please enter a starting total." << endl;
    cin >> total;

    while (total < 1){    //Can also be (total <=0)
        cout << "The starting total must be higher than one." << endl;
        cout << "Please re-input a new starting total and press enter." << endl;
        cin >> total;
    }

    cout << "Please input the highest amount you would like ";
    cout << "to be able to subtract and press enter." << endl;
    cin >> total_subtract;

    while (total_subtract >= total){
        cout << "The highest amount you would like to be able to";
        cout << " subtract must be lower than your starting total." << endl;
        cout << "Please re-input a new amount to subtract and press enter." << endl;
        cin >> total_subtract;
    }

    while (total > 0){    //Can also be (total >= 1) or (true)******
        if (total % 3 == 2){
            total = total - 2;
            cout << "I am subtracting 2." << endl;
            cout << "The new total is " << total << "." << endl;
        }else{
            total = total - 1;    //Can also be total--
            cout << "I am subtracting 1." << endl;
            cout << "The new total is " << total << "." << endl;
        }

    //In the book's code they wrote (total == 0), but in the pseudocode they
    //wrote if the total is 0 OR LESS. Therefore I wrote (total <= 0).
        if (total <= 0){    //Can also be (total < 1)
            cout << "I win!" << endl;
            break;
        }

        cout << "Please enter the amount you wish to subtract ";
        cout << "and press ENTER." << endl;
        cin >> subtract;

    //In the book's code they wrote (subtract < 1 || subtract > 2), but in the
    //pseudocode they wrote while input IS NOT 1 or 2. Therefore I wrote:
        while (subtract < 1 || subtract > total_subtract){
            cout << "Please re-enter the amount you wish to subtract ";
            cout << "and press ENTER." << endl;
            cout << "The amount must be between 1 and ";
            cout << total_subtract << " ." << endl;
            cin >> subtract;
        }

        total = total - subtract;
        cout << "The new total is " << total << "." << endl;

    //In the book's code they wrote (total == 0), but in the pseudocode they
    //wrote if the total is 0 OR LESS. Therefore I wrote (total <= 0).
        if (total <= 0){    //Can also be (total < 1)
            cout << "You win!" << endl;
          //  break;
        }
    cout << "Would you like to play again?" << endl;
    cout << "If yes, type 1. If not, type 0." << endl;
    cin >> play_again;


        if (play_again == 1){
            cout<< "Great! I love this game!" <<endl;
        }
      if (play_again == 0){
            break;
            }
      
    }
   
     break;

}
   
//system("PAUSE");
return 0;
}

[code]
Last edited on
add } before cout << "Would you like to play again?" << endl;.

And delete
1
2
break;
}
before
1
2
3
//system("PAUSE");
return 0;
}


EDIT : Why the second code you don't place it in code tag =..=?
Last edited on
lsk asks: EDIT : Why the second code you don't place it in code tag =..=?

I could not figure out how. When I posted the original post the "code-/code" was already in the window. When I replied to Genado, there were no code/code & I had no idea how to make them appear & know that/if I could just type them. I just spent time looking thru the faq & "how to post" rants, but nowhere do I see any "how to actually use the forum interface".

I saw the format buttons but only focused on the B, I & U, so I assumed it was all font related & simply never thought about the <> being what I was looking for. What is obvious to long time forum users is not at all so to newbies. Not sure what made me poke the <> button, but I did go back & add to the other post as well, FWIW.

At any rate I am happy to report the program is fixed & works very well. I left the old code in (but commented out) for the next guy or gal who uses the book, as this is the one program (so far) that they did not supply as answer code for.

I kept explaining to the student that just because the error is at the end, that its only because that is where the compiler got to when it realized there was a problem, but that the error itself is not necessarily 1 or 2 lines above return. This was perfect example showing the missing brace was half way up the program & I just could not spot it but I was sure someone here would be able to.

Thanks to both of you, as both answers were correct, we just did not quite understand the first one. Here is the final code:
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
using namespace std;

int main(){

while(true)
{

    int total;
    int subtract;
    int total_subtract;
    int play_again;

    cout << "Welcome to The Subtraction (NIM) Game." << endl;
    cout << "Please enter a starting total." << endl;
    cin >> total;

    while (total < 1)
	{    //Can also be (total <=0)
        cout << "The starting total must be higher than one." << endl;
        cout << "Please re-input a new starting total and press enter." << endl;
        cin >> total;
    }

    cout << "Please input the highest amount you would like ";
    cout << "to be able to subtract and press enter." << endl;
    cin >> total_subtract;

    while (total_subtract >= total)
	{
        cout << "The highest amount you would like to be able to";
        cout << " subtract must be lower than your starting total." << endl;
        cout << "Please re-input a new amount to subtract and press enter." << endl;
        cin >> total_subtract;
    }

    while (total > 0)
	{    //Can also be (total >= 1) or (true)
        if (total % 3 == 2)
		{
            total = total - 2;
            cout << "I am subtracting 2." << endl;
            cout << "The new total is " << total << "." << endl;
        }
		else
		{
            total = total - 1;    //Can also be total--
            cout << "I am subtracting 1." << endl;
            cout << "The new total is " << total << "." << endl;
        }

		//In the book's code they wrote (total == 0), but in the pseudocode they
		  //wrote if the total is 0 OR LESS. Therefore I wrote (total <= 0).
        if (total <= 0)
		{    //Can also be (total < 1)
            cout << "I win!" << endl;
            break;
        }

        cout << "Please enter the amount you wish to subtract ";
        cout << "and press ENTER." << endl;
        cin >> subtract;

    //In the book's code they wrote (subtract < 1 || subtract > 2), but in the
    //pseudocode they wrote while input IS NOT 1 or 2. Therefore I wrote:
        while (subtract < 1 || subtract > total_subtract)
		{
            cout << "Please re-enter the amount you wish to subtract ";
            cout << "and press ENTER." << endl;
            cout << "The amount must be between 1 and ";
            cout << total_subtract << " ." << endl;
            cin >> subtract;
        }

        total = total - subtract;
        cout << "The new total is " << total << "." << endl;

    //In the book's code they wrote (total == 0), but in the pseudocode they
    //wrote if the total is 0 OR LESS. Therefore I wrote (total <= 0).
        if (total <= 0)
		{    //Can also be (total < 1)
            cout << "You win!" << endl;
            break;
        }
	} 				//*****added closing bracket here to close while
	cout << "Would you like to play again?" << endl;	
    	cout << "If yes, type 1. If not, type 0." << endl;
    	cin >> play_again;


        if (play_again == 1)
		{
        } 
		else 
		{
        //(play_again == 0){  (****no change here--this line is still commented out
        //    break;                (****this line commented out- not needed)
        // }		           (**** also not needed 
    

    }
}

//system("PAUSE");
return 0;
}


BTW, I still don't see the edit & Run next to my code boxes & I have no idea what to do to add that either.

I see some one is hard at work on a FAQ section, so perhaps someone can suggest adding a small section on how to use this interface effectively.
Last edited on
Topic archived. No new replies allowed.