Program isn't reading my ending values

So I'm trying to set up my game of NIM so that once the game is over, it asks if you'd like to play again with either a 1 or a 0. I have an if statement set up to do so, but right now, even if I do enter a 1 or a 0, it just returns the "invalid statement" option that happens earlier in my program. Here's a screenshot of what's happening.

https://imgur.com/a/SaKqO

And here is the code.

Does anyone know what I'm doing wrong here? I feel like it has something to do with the order of my functions.

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  /* The game of NIM */
#include <iostream>
#include <cstdlib>
using namespace std;

int menu();
int game();

int main()
{
	menu();
}

int menu()
{
	int choice;
	cout << "Would you like to play NIM? Enter 1 to play, or enter 0 to exit.\n";
	cin >> choice;

	if (choice == 1)
	{
		game();
	}

	if (choice == 0)
	{
		return 0;
	}

	else
	{
		cout << "Invalid entry. Please enter either a 1 or a 0.\n";
		menu();
	}
}

int game()
{

		cout << "Welcome to the game of NIM!\n";
		cout << "---------------------------\n";
		cout << "You will take turns against a computer,\nremoving anywhere from 1-3 objects from the game.\n";
		cout << "Whoever ends up removing the last object from the game loses,\nand the other player wins!\n\n";

	int num_objects = 23;
	int current_player = 1;
	int move;
	int minimum_move = 1;
	int maximum_move = 4;
	int o = 0;
	

	do
	{
		if (current_player == 1)
		{
			cout << "Player 1, enter your move (1-4): ";
			cin >> move;
			while (move < minimum_move || move > maximum_move || move > num_objects)
			{
				cout << "Illegal move. \nEnter a new move: ";
				cin >> move;
			}
		}
		
		else
		{

			switch (num_objects)
			{
			case 1: move = 1;
				break;
			case 2: move = 1;
				break;
			case 3: move = 2;
				break;
			case 4: move = 3;
				break;
			case 5: move = 4;
				break;
			default: move = 1 + rand() % 4;
				break;
			}

			/*do
			{
				move = 1 + rand() % 3;
			}
			*/
			while (move < minimum_move || move > maximum_move || move > num_objects);
			cout << "Computer removed " << move << endl;
		}

		num_objects = num_objects - move;
		for (o = 0; o < num_objects; ++o)
		{
			cout << "*";
		}
		cout << "\n" << num_objects << " objects remaining.\n";

		if (num_objects > 15 && num_objects < 19)
		{
			cout << "Things are getting pretty intense in here!\n";
		}

		if (num_objects < 7 && num_objects > 3)
		{
			cout << "This is a close game!\n";
		}

		current_player = (current_player + 1) % 2;
	}

	while (num_objects > 0);


	if (current_player == 1)
	{
		cout << "Player 1 wins! Good job! :D\n";
	}
	
	else
	{
		cout << "The computer wins! Good try!\n";
	}

	cin.ignore();
	cout << "\nIf you'd like to play again, enter 1. If you would like to exit, enter 0.\n";
	
	int play_again;

	cin >> play_again;

	if (play_again == 1)
	{
		game();
	}

	if (play_again == 0)
	{
		return 0;
	}
}
Hi friend,

Look at line 22 in the menu function. Just after the call to game(); add this line...

return 0;

Good luck to you.
Last edited on
Is there any particular reason for that? It worked successfully, but I'm not entirely sure why...I thought that return 0; closed out of the program.
the other way to fix is to put else in front of line 25.

Your code as-is, if the user enters "1" , enters the game() function, and then evaluates the conditional on line 25. What you really want is for it to exclusively enter one of the three conditional sections, and so you want a
1
2
3
4
5
6
7
8
9
if (...)
{
}
else if (...)
{
}
else
{
}
yes, return 0 is used to close the program, but that is what you wanted it to do, no?

Notice how in the menu function, you made a call to game function.
after you complete the game function, the program goes back to the menu function where you left off. But you meant for it to close out at that point. Thus, the return 0 works.
Topic archived. No new replies allowed.