im having major issues saving my TicTacToe game in C++.

it is mainly in the function to save the game.. im not sure also if the code is calling that function properly. il show the code.......:

{
                char save_or_not;
                cout<<"Would you like to save the game?"<<endl;
                cin>>save_or_not;
                if(save_or_not == 'y'|| save_or_not == 'Y')
                {
                    void Save_Game();
                    break;
                }
                else
                {
                    break;
                }
                .
                .
                .
                .
                .
void Game::Save_Game(char Board[3][3])
{
    int i;
	FILE *savegame = fopen("save.bin","w");
	for (i = 0; i < 3; i++)
    {
        fprintf(savegame,"%c %c %c\n", Board[i][0], Board[i][1], Board[i][2]);
    }
	fclose(savegame);
	cout<<"game saved";
}

note: Board is the array for the board.
 I am a beginner at C++ so any help in relation to this program i assure you i can thank enough. So thanks in advance.

Dani AI

Generated

Two quick, concrete fixes tied to what posted and suggested: the line void Save_Game(); inside your if-block is a function declaration, not a call — remove the void and call the member function (for example this->Save_Game(board); or game.Save_Game(board);) before you break. Also save the game state and the simple stats (wins/losses/draws) together so loading restores the exact game state, as recommended.

A minimal, robust text-based save/load using C++ streams (safer than raw C FILE* for beginners) — write three board rows then stats. Example save and load implementations:

void Game::Save_Game(const char board[3][3], int wins, int losses, int draws)
{
    std::ofstream out("tictactoe_save.txt");
    if (!out) { std::cerr << "Cannot open save file\n"; return; }
    for (int r = 0; r < 3; ++r)
        out << board[r][0] << board[r][1] << board[r][2] << '\n';
    out << wins << ' ' << losses << ' ' << draws << '\n';
}

bool Game::Load_Game(char board[3][3], int &wins, int &losses, int &draws)
{
    std::ifstream in("tictactoe_save.txt");
    if (!in) return false;
    std::string line;
    for (int r = 0; r < 3 && std::getline(in, line); ++r)
        for (int c = 0; c < 3 && c < (int)line.size(); ++c)
            board[r][c] = line[c];
    in >> wins >> losses >> draws;
    return true;
}

Using std::ofstream/std::ifstream (RAII, error checking) is recommended. (en.cppreference.com)

Notes and gotchas: if you intended a binary save, open with the binary flag (e.g., std::ios::binary) or use "wb" with fopen on Windows — plain "w" opens for text and will truncate the file. Also beware that plain char[3][3] decays to a pointer when passed; consider std::array or passing by reference for clearer semantics. (en.cppreference.com)

Recommended Answers

All 7 Replies

>>void Save_Game();

This is not how to call a function.

Since your Save_Game takes an 2d array, you need call your function
like this :

Save_Game( gameBoard) ; //where gameBoard is the board of tic-tac-toe

have you any ideas on the save part?

have you any ideas on the save part?

Yes.

1) You need to save your game board
2) You need to save stats : wins, losses, draw games.

Then when you load it in, all you have to do is
fill your game board, and your stats variable.

Yes.

1) You need to save your game board
2) You need to save stats : wins, losses, draw games.

Then when you load it in, all you have to do is
fill your game board, and your stats variable.

anything in the way of code?

do you know how to open a file to save it.

do you know how to open a file to save it.

i think so..

i have
FILE *savegame = fopen("save.bin","w");

is that right..
by the way thanks for the help. and your patience i suppose.. not easy helpin us "amateurs"

Use to save a file. Read up a bit on it and tell me if you know
how to open a file for saving.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.