0

I am trying to make a tic-tac-toe game. I made this function which supposed to update the board values (insert 'X' or 'O') but the struct simply doesn't update, it shows that the values stay the same as initialized.

#include <iostream>

struct Board {//initializes a struct
    char A1 = ' ';
    char A2 = ' ';
    char A3 = ' ';
    char B1 = ' ';
    char B2 = ' ';
    char B3 = ' ';
    char C1 = ' ';
    char C2 = ' ';
    char C3 = ' ';
};

struct Board InputSignInStruct(char num, char letter, struct Board b,char sign) {//a function which is supposed to update values in the struct
    if (letter == 'A')
    {
        if (num == '1')
            b.A1 = sign;
        if (num == '2')
            b.A2 = sign;
        if (num == '3')
            b.A3 = sign;
    }
    if (letter == 'B')
    {
        if (num == '1')
            b.B1 = sign;
        if (num == '2')
            b.B2 = sign;
        if (num == '3')
            b.B3 = sign;
    }
    if (letter == 'C')
    {
        if (num == '1')
            b.C1 = sign;
        if (num == '2')
            b.C2 = sign;
        if (num == '3')
            b.C3 = sign;
    }
    return b;
}

int main() {
    struct Board b;
    char letter,num;
    std::cin>>letter>>num;
    char sign;
    std::cin>>sign;
    b = InputSignInStruct(letter, num, b, 'X');//should return the updated struct but it doesent update
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • 1
    Why do you say so? – Jean-Baptiste Yunès Dec 10 '18 at 15:10
  • 1
    OT: you should rewrite this code immediately and use a 3x3 array rather than your `struct Board` which will considerably reduce the quantity of code and the quality of your code. Consider what would happen if all of a sudden you need a 10x10 board rather than a 3x3 one. – Jabberwocky Dec 10 '18 at 15:12
  • 2
    Please check what you get for `letter` and `num`. I'm guessing therein lies the problem. – srdjan.veljkovic Dec 10 '18 at 15:25
  • If you think your trouble is with the function, post a minimal example which reproduces the problem with the function but with known input, i.e. not relying on `cin`. Also, make sure your code compiles before posting it. – Daniel Daranas Dec 10 '18 at 15:26
  • Dear Artem, welcome to Stack Overflow. I took the liberty to update your question a bit. Please check if this still satisfies your intentions. If not, feel free to revert the edit. – kvantour Dec 10 '18 at 15:27
  • 1
    Why are you passing a struct to the function? It looks like it should be a local variable in the function. (And you can just write `Board`; you don't need to repeat `struct` in C++.) – molbdnilo Dec 10 '18 at 15:43

2 Answers2

3

Your function definition is

struct Board InputSignInStruct(char num, char letter, ...)

But you are calling as

b = InputSignInStruct(letter, num ...)

You have interchanged num and letter.

balki
  • 26,394
  • 30
  • 105
  • 151
1

You need to match you function call values with the definition.

Change this:

b = InputSignInStruct(letter, num, b, 'X');/

To this:

b = InputSignInStruct(num, letter, b, 'X');/