0

edit: this is how my output when I input number

Please enter the move: 
1
X--
---
---
Please enter the move: 
2
-X-
---
---
Please enter the move: 
3
--X
---
---
Please enter the move: 
4
---
X--
---
Please enter the move: 

The change is not saved.

I'm trying to change the array using the function that input by a user. It does get the input but it doesn't affect anything to my array outside of the function.

I've tried all different approaches

void (char *array[]) or void(char array[][3]) or void(char **array)

None of them worked.

#include <iostream>
#include <sstream>
#include <fstream>
#include <string.h>
#include <random>

using std::string;
using std::getline;
using namespace ::std;

const string winningCases[8] = {"123","456","789","147","258","369","159","357"};


void make_board(char grid[3][3]){
  // some code which works
}

void print_board(char grid[3][3]){
  // some code which works
}

void enter_move(char grid[][3]){
    char humanMove;
    int num_humanMove;

    while(true){
        cout << "Please enter the move: " << endl;
        cin >> humanMove;

        // find index for a grid
        num_humanMove = static_cast<int>(humanMove) - 49;

        int row = num_humanMove / 3;
        int col = num_humanMove % 3;

        // check right input
        if(49 > static_cast<int>(humanMove) && static_cast<int>(humanMove) < 57){
            cout << "Not valid input. " << endl;

        }else if(grid[row][col] == 'X' || grid[row][col]== 'O'){
            cout << "It's taken. " << endl;

        }else{
            grid[row][col] = 'X';

//            print_board(*grid[3]);

            break;
        }
    }

}

int find_grid_space(char move){
  // some code which works
}

char continue_play(){
  // some code which works
}



int main(int argc, char *argv[]){

    char grid[3][3];


    char play='y';
    bool win=true;
    while(play == 'y' || play == 'Y'){
        while(win){

            make_board(grid);
            print_board(grid);

            enter_move(grid);

            win = !check_for_win(grid);

        }
        play = continue_play();

    }

    return 0;
}

So, the function void enter_move(char grid[][3]) should get the input from the user and change the grid. It changes the grid in the function but it won't do anything outside of the function.

  • No, that code works (I'm talking about `enter_move`). Whatever your problem is, it's not what you think it is. – john Jul 24 '19 at 05:01
  • Possible duplicate of [Passing a 2D array to a C++ function](https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function) –  Jul 24 '19 at 05:03
  • 2
    @Chipster It might be a duplicate, but the thing is that he's doing it right already. – john Jul 24 '19 at 05:04
  • @john fair point. Maybe should be closed for not being clear what isn't working about these solutions? –  Jul 24 '19 at 05:05
  • 1
    We see this all the time, newbie has a problem with some code, and thinks they know where the problem is. But often they are wrong. So it might help if you also posted why you think this code doesn't change `grid` outside the function. It's there that you've probably made your mistake, because `enter_move` looks fine. – john Jul 24 '19 at 05:06
  • Well it doesn’t work on my laptop.. would that be a problem too?? –  Jul 24 '19 at 05:06
  • 2
    Have you tried debugging your programme? That should always be your first choice if you encounter trouble... – Aconcagua Jul 24 '19 at 05:09
  • 1
    Try and see it from our point of view, so far you've posted some perfectly correct looking code and all you've said is 'it doesn't work'. – john Jul 24 '19 at 05:11
  • oh @john I'll stop just saying 'it doesn't work' haha so in my code, it doens't print out what it supposed to print. for example, when I put 1, it should print `1--\n---\n---` but it will only print `---\n---\n---`. I don't think it's other function's problem since these are using same `grid[3][3]`. I might be wrong.. more likely probably lol –  Jul 24 '19 at 05:13
  • OK, I'm going to take a guess at the problem, answer soon. – john Jul 24 '19 at 05:14
  • Note that your *check right input* does not seem correct – Damien Jul 24 '19 at 05:16
  • 1
    Shouldn't it print `X--\n---\n---\n`? You call `make_board` within your while loop – I'd guess that you overwrite the previously modified board this way. Suppose you should move the call to this function in front of the `while(win)` loop. – Aconcagua Jul 24 '19 at 05:16
  • @Aconcagua Geez.. you're right. I feel so stupid. Ok, so `make_board` was in a second `while` loop and it did overwrite my board. I moved that to the first `while` loop and problem solved. Thank you and sorry everyone who was trying to help. –  Jul 24 '19 at 05:21
  • @Damien hmm how not correct? –  Jul 24 '19 at 05:26
  • I guess you want to check if the input is not in a given range, 49-56, it is not what you are doing – Damien Jul 24 '19 at 05:30
  • Are you aware that you can use character literals for comparison? `if('1' <= x && x <= '9') { /*correct input */ }`? That would be safe even on systems not using ASCII-compatible encoding ('0' ... '9' are guaranteed to be subsequent by C-standard – which doesn't apply for other symbols). – Aconcagua Jul 24 '19 at 05:31

1 Answers1

2

The problem seems to be here

   while(win){
        make_board(grid);
        print_board(grid);
        enter_move(grid);
        win = !check_for_win(grid);
    }

Each time round the loop you call make_board which I'm guessing resets the board each time.

What you should have is this

   make_board(grid);
   while(win){
        print_board(grid);
        enter_move(grid);
        win = !check_for_win(grid);
    }

so that you only set up the board once.

john
  • 71,156
  • 4
  • 49
  • 68