0

I'm quite new to C++ and coding in general and I have been stuck on this bug for forever it seems.

My eventual goal is to create a tic-tac-toe algorithm but at the moment I am having an issue with using struct variables outside of the struct.

I have tried using classes and structs, using static etc., I know I am missing something I just don't know what it is.

Heres the code, it's not exactly beautiful but I'm pretty sure it should work.

#include <iostream>
#include <string>

int userResponse;

//Class to monitor board positions 
struct boardPos
{
    bool Pos1 = 0;
    bool Pos2 = 0;
    bool Pos3 = 0;
    bool Pos4 = 0;
    bool Pos5 = 0;
    bool Pos6 = 0;
    bool Pos7 = 0;
    bool Pos8 = 0;
    bool Pos9 = 0;
};

//Changing bool values
void boolChange()
{
    if (userResponse == 1)
    {
        boardPos::Pos1 = 1;
    }

    if (userResponse == 2)
    {
        boardPos::Pos2 = 1;
    }

    if (userResponse == 3)
    {
        boardPos::Pos3 = 1;
    }

    if (userResponse == 4)
    {
        boardPos::Pos4 = 1;
    }
}




//std::string A, B, C, D, E, F, G, H, I

int main()
{

//Variable to Print Position Board
std::string posBoard = "\n 1 | 2 | 3\n-----------\n 4 | 5 | 6\n-----------\n 7 | 8 | 9\n";
std::cout << posBoard;

std::cout << "Enter Position Number\n";
std::cin >>  userResponse;
}


stapler
  • 13
  • 1
  • You need an instance of `boardPos` to access non static member variables. – drescherjm Sep 16 '21 at 14:45
  • Although 0 or 1 will work a bool is supposed to be `true` or `false` – drescherjm Sep 16 '21 at 14:51
  • 1
    Do you know what a struct does? Or did someone (a teacher, perhaps) just tell you "put your variables in a struct" and not follow up with any good instruction on how to use such a feature? – Silvio Mayolo Sep 16 '21 at 14:53
  • An observation: If you had been stuck on this bug forever, you could hardly call yourself new to C++. – user4581301 Sep 16 '21 at 15:32
  • "I'm pretty sure it should work." [It doesn't](https://coliru.stacked-crooked.com/a/934e024da6b67883). Not only does it not compile, but you never call `boolChange` – Caleth Sep 16 '21 at 15:57

3 Answers3

0

You need to refer to an object of some struct, not sturct itself.

 void boolChange(boardPos &b)
{
    if (userResponse == 1)
    {
        b.Pos1= 1;
    }

    if (userResponse == 2)
    {
        b.Pos2 = 1;
    }

    if (userResponse == 3)
    {
        b.Pos3 = 1;
    }

    if (userResponse == 4)
    {
        b.Pos4 = 1;
    }
}

Notice that the function definition has changed, now it takes a reference to an object of boardPos. Now you need to create an object of boardPos and then pass it by reference to boolChange()

Michał Turek
  • 701
  • 3
  • 21
0

You need an instance in order to change the values of it.

boardPos bPos;

void boolChange(boardPos& _boardPos)
{
    if (userResponse == 1)
    {
        _boardPos.Pos1 = 1;
    }

    if (userResponse == 2)
    {
         _boardPos.Pos2 = 1;
    }

    if (userResponse == 3)
    {
         _boardPos.Pos3 = 1;
    }

    if (userResponse == 4)
    {
         _boardPos.Pos4 = 1;
    }
}

int main()
{
    boolChange(bPos);
}

Or you could have declared every member of your struct as static;

struct boardPos
{
    static bool Pos1;
    .
    .
    .
    static bool Pos7;
    static bool Pos8;
    static bool Pos9;
};
//NOTE: you have to define the static members of struct outside

static bool boardPos::Pos1 = 0;
.
.
.
static bool boardPos::Pos9 = 0;
Karen Baghdasaryan
  • 2,407
  • 6
  • 24
  • 1
    None are broken here, but make sure you know [the underscore rules](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) before you start using them heavily. – user4581301 Sep 16 '21 at 15:34
0

I have made some changes and below is the complete working program. You can see that depending on the user input the corresponding Pos data member is changed.

#include <iostream>
#include <string>

//Class to monitor board positions 
struct boardPos
{
    bool Pos1 = 0;
    bool Pos2 = 0;
    bool Pos3 = 0;
    bool Pos4 = 0;
    bool Pos5 = 0;
    bool Pos6 = 0;
    bool Pos7 = 0;
    bool Pos8 = 0;
    bool Pos9 = 0;
};

//Changing bool values
void boolChange(int userResponse, boardPos &tempbPos)
{
    if (userResponse == 1)
    {
        tempbPos.Pos1 = 1;
    }

    if (userResponse == 2)
    {
        tempbPos.Pos2 = 1;
    }

    if (userResponse == 3)
    {
        tempbPos.Pos3 = 1;
    }

    if (userResponse == 4)
    {
        tempbPos.Pos4 = 1;
    }
}




int main()
{
    int userResponse;
    
    //create struct variable/object whose data member you want to change
    boardPos bPos;
    //lets check the pos member values before calling the function boolChange. Note all Pos members are 0
    std::cout<<"pos1: "<<bPos.Pos1<<" pos2: "<<bPos.Pos2<<" pos3: "<<bPos.Pos3<<" pos4: "<<bPos.Pos4<<std::endl;
    
    
    //Variable to Print Position Board
    std::string posBoard = "\n 1 | 2 | 3\n-----------\n 4 | 5 | 6\n-----------\n 7 | 8 | 9\n";
    std::cout << posBoard;

    std::cout << "Enter Position Number\n";
    std::cin >>  userResponse;
    boolChange(userResponse, bPos);//note here we have passed bPos by reference
    
    //lets check if the pos member is changed. Now since we have called boolChange(), note corresponding Pos member value has changed
    std::cout<<"pos1: "<<bPos.Pos1<<" pos2: "<<bPos.Pos2<<" pos3: "<<bPos.Pos3<<" pos4: "<<bPos.Pos4<<std::endl;
    
    
}

Jason
  • 36,170
  • 5
  • 26
  • 60