Hi guys, I am trying to make the Tic Tac Toe Class game but I am encountering an error with the initialization of my array for the game board. Instead of initializing everything to blank spaces, random characters end up in there and I don't know what is causing this problem.

Here is my code beginning with the header file:

#ifndef TTT_H
#define TTT__H

class TTT
{

public:

TTT();
~TTT();
void drawBoard(char tttBoard[9]);
void makeMoveX(char tttBoard[9]);
void makeMoveO(char tttBoard[9]);
void checkWinnerX(char tttBoard[9], char z, int& win);
void checkWinnerO(char tttBoard[9], char u, int& win);

private:

char tttBoard[9];
int round;
int win;
};
#endif
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "ttt.h"

using namespace std;


//constructor
TTT::TTT()
{
        round = win = 0;
        tttBoard[0] = ' ';
        tttBoard[1] = ' ';
        tttBoard[2] = ' ';
        tttBoard[3] = ' ';
        tttBoard[4] = ' ';
        tttBoard[5] = ' ';
        tttBoard[6] = ' ';
        tttBoard[7] = ' ';
        tttBoard[8] = ' ';
}


//destructor
TTT::~TTT()
{
        cout<<"Destructor called";
}


void TTT::drawBoard(char tttBoard[9])
{
    cout<<"\n +---+---+---+";
    cout<<"\n | "<<tttBoard[0]<<" | "<<tttBoard[1]<<" | "<<tttBoard[2]<<" | ";
    cout<<"\n +---+---+---+";
    cout<<"\n | "<<tttBoard[3]<<" | "<<tttBoard[4]<<" | "<<tttBoard[5]<<" | ";
    cout<<"\n +---+---+---+";
    cout<<"\n | "<<tttBoard[6]<<" | "<<tttBoard[7]<<" | "<<tttBoard[8]<<" | ";
    cout<<"\n +---+---+---+";
}

void TTT::makeMoveX(char tttBoard[9])
{
    int s;
        bool repeat = false;
    cout<<"\nPlayer x, enter the square number you wish to play: ";
    do {
                cin>>s;
                if ((s>0)&&(s<=9))
                {
                        if ((!(tttBoard[s-1]=='X'))&&(!(tttBoard[s-1]=='O')))
                        {
                                tttBoard[s-1]='X';
                                round++;
                                repeat = true;
                        }
                        else
                        {
                                cout<<"\n Enter an empty square number ";
                        }
                }


                else
                {
                        cout<<"\n Enter a valid square number(1-9) : ";
                }
        } while (repeat == false);


}

void TTT::makeMoveO(char tttBoard[9])
{
    int v;
        bool repeat = false;
    cout<<"\nPlayer O, enter the square number you wish to play: ";
    do {
                cin>>v;
                if ((v>0)&&(v<=9))
                {
                        if ((!(tttBoard[v-1]=='O'))&&(!(tttBoard[v-1]=='X')))
                        {
                                tttBoard[v-1]='O';
                                round++;
                                repeat = true;
                        }
                        else
                        {
                                cout<<"\n Enter an empty square number ";
                        }
                }


                else
                {
                        cout<<"\n Enter a valid square number(1-9) : ";
                }
        } while (repeat == false);


}


void TTT::checkWinnerX(char tttBoard[9], char z, int& win)
{

        for (int i=0; i<7; i+=3)
        {
                if (((tttBoard[i]==z)&&(tttBoard[i+1]==z))&&((tttBoard[i+1]==z)&&(tttBoard[i+2]==z)))
                        win = 1;
        }
        for (int j=0; j<3; j++)
        {
                if (((tttBoard[j]==z)&&(tttBoard[j+3]==z))&&((tttBoard[j+3]==z)&&(tttBoard[j+6]==z)))
                        win = 1;
        }
        if ((tttBoard[0]==z)&&(tttBoard[4]==z)&&(tttBoard[8]==z))
                win=1;
        if ((tttBoard[2]==z)&&(tttBoard[4]==z)&&(tttBoard[6]==z))
                win=1;
}


void TTT::checkWinnerO(char tttBoard[9], char u, int& win)
{

        for (int i=0; i<7; i+=3)
        {
                if (((tttBoard[i]==u)&&(tttBoard[i+1]==u))&&((tttBoard[i+1]==u)&&(tttBoard[i+2]==u)))
                        win = 2;
        }
        for (int j=0; j<3; j++)
        {
                if (((tttBoard[j]==u)&&(tttBoard[j+3]==u))&&((tttBoard[j+3]==u)&&(tttBoard[j+6]==u)))
                        win = 2;
        }
        if ((tttBoard[0]==u)&&(tttBoard[4]==u)&&(tttBoard[8]==u))
                win=2;
        if ((tttBoard[2]==u)&&(tttBoard[4]==u)&&(tttBoard[6]==u))
                win=2;
}

If anyone could help me I would really really appreciate it, thank you

Without seeing the driver program it's hard to tell, but the design seems flawed. These functions shouldn't take the char[] parameter since they are member functions. Your char[] array is thus passed with the object, so you don't need it as a parameter. Thus I would imagine you would want this:

drawBoard()

I'd have to see the driver to see how you are calling them, but my hunch is that the address of the char[] parameter is different from the this->tttBoard parameter. Unless there is a real reason to pass the char[] array, and I can't think of any reason the the drawBoard() function would need it since it is a class functon, don't pass it.

Ahh you know why I did that? Before this I had to make a TTT game work without using a class, so it was basically code from that that I just copy pasted over without really thinking about it. I took your advice and it now initializes correctly, problem solved now and I should be able to work through this just fine, thanks amigo.

This question has already been answered. Start a new discussion instead.