Line won't print

Mar 15, 2015 at 7:35pm
I am practicing c++ out of a book and it had me create the game NIM. One exercise tells me to change the program so that it prints the number of objects removed by the player, so I added line 27, but it will not print line 27. What did I do wrong with the cout?

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
  /*      The game of NIM         */
#include<iostream>      // include two libraries
#include<cstdlib>
using namespace std;
int main()              // main starts the actual program
{
        //--------------------variable declarations-------------------------
        int num_objects = 23;
        int current_player = 1;
        int move;
        //-------------------- Beginning of the game loop -------------------
        cout << "How to play NIM: " << endl;
        cout << "The traditional game of Nim is played with a number of coins arranged in heaps: the number of coins and heaps is up to you. " << endl;
        cout << " There are two players. When it's a player's move he or she can take any number of coins from a single heap. They have to take at least" << endl;
        cout << " one coin, though, and they can't take coins from more than one heap. The winner is the player who makes the last move, so there are no" << endl;
        cout << "coins left after that move. (Some people play the game the other way around, with the last person to make a move losing the game, but we'll" << endl;
        cout << " ignore that version for the moment." << endl;
        cout << " It's clear that there is no luck involved here. You can work out the best move to make by cleverly predicting the sequence of moves that would follow it." << endl;

        do {
           if (current_player == 1) {                                           // conditional: if
                cout << "Player 1, enter your move (1-3): ";            // output
                cin >> move;                                                    // input
                while (move < 1 || move > 3 || move > num_objects) {
                cout << "Illegal move. \nEnter a new move: ";
                cin >> move;
                cout << "Player 1 removed " << move << endl;
              }
           } else {                                                             // else part of conditional
                do {                                                            // make sure move is legal
                   move = 1 + rand() % 3;                                       // random computer move
                } while (move < 1 || move > 3 || move > num_objects);
                cout << "Computer removed " << move << endl;
           }
           num_objects = num_objects - move;                                    // implement the move
           cout << num_objects << " objects remaining. \n";
           current_player = (current_player + 1) % 2;                           // switch player
        } while (num_objects > 0);
        //--------------end of the game loop-----------------
        cout << "Nice going player " << current_player << ", you win!\n";
        cin.ignore();
        cout << "\nPress enter to quit.\n";
        cin.ignore();
        return 0;
}
Last edited on Mar 15, 2015 at 7:39pm
Mar 15, 2015 at 7:43pm
Works fine for me. When I enter 6 as my move, and then any other move, then it prints out line 27.

What do you want it to do, becuase right now its doing exactly what you are telling it to.
Mar 15, 2015 at 7:50pm
I just want it to tell the number of objects removed by the player. Is that what it is doing for you?
Mar 15, 2015 at 7:54pm
First it checks if the move was illegal - while (move < 1 || move > 3 || move > num_objects)

If it is, say I enter 7, which is an illegal move, then it will enter the loop. Then it will ask the user to enter another re-enter the move. If the user enters something illegal again, it will then print out line 27, which is "Player 1 removed" etc. and then re-run the loop. If the user enters something legal, it will STILL print out line 27.

You probably want it to only output how line 27 when the user makes a legal move no?
Mar 15, 2015 at 7:57pm
Oh, yes. I added the same cout right before the while loop on line 24 and it works. Thanks
Mar 15, 2015 at 8:07pm
Good good :) But, remember, it will also tell us how many player removed even if we give the program a illegal move, thats not what you want is it?
Mar 17, 2015 at 2:08am
No I do not want it to tell how many the player moved unless it is a legal move. So am I going to need another if statement right after the "cin >> move;" inside my first if statement that will tell it to only say the amount that the player removed if the move is within 1 and 3?
Last edited on Mar 17, 2015 at 2:13am
Topic archived. No new replies allowed.