Tic Tac Toe program on C++

I've been learning C++ as a hobby for the previous month and recently i started on a project to make this Tic Tac Toe game. I've written the source code below. It isn't complete - i've only just started on it. The problem is that I can't get the functions called "putX" and "isLegal" to work - the program just freezes.
I'd appreciate it if someone could tell me where i've gone wrong and also how i can make the program more efficient.
Please remember that this is not the final form of the program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
using namespace std;

void setGrid(char [8]);
void printGrid(char [8]);
int getMove(int);
void putX(int, char [8]);
bool isLegal(int, char[8]);
int getMove(int, char [8]);


int main()
{
    int cellnumber;
    char playGrid[8];
    
    setGrid(playGrid);
    printGrid(playGrid);
    getMove(cellnumber);
    putX(cellnumber, playGrid);
    printGrid(playGrid);
    system ("pause");
    return 0;
}

void setGrid(char playGrid[8])
{
     playGrid[0]='1'; playGrid[1]='2'; playGrid[2]='3';
     playGrid[3]='4'; playGrid[4]='5'; playGrid[5]='6';
     playGrid[6]='7'; playGrid[7]='8'; playGrid[8]='9';
}

void printGrid(char playGrid[8])
{
     cout<<playGrid[0]<<" | "<<playGrid[1]<<" | "<<playGrid[2]<<endl;
     cout<<"_________"<<endl;
     cout<<playGrid[3]<<" | "<<playGrid[4]<<" | "<<playGrid[5]<<endl;
     cout<<"_________"<<endl;
     cout<<playGrid[6]<<" | "<<playGrid[7]<<" | "<<playGrid[8]<<endl;
     
}

int getMove(int cellnumber)
{
    do
    {
                cout<<"Enter the number of the cell where you want to make your move: "; cin>>cellnumber;
                cellnumber--; 
                if(cellnumber<0 || cellnumber >9) {cout<<"\aThis cell does not exist"<<endl; continue;}
    }while (!isLegal);
    return cellnumber;
}

bool isLegal(int cellnumber,char playGrid[8])
{
     if (playGrid[cellnumber]=='x') return false;
     else return true;
}

void putX(int cellnumber, char playGrid[8])
{
     playGrid[cellnumber]='x';
}

Here are your problems:

*getMove takes a cellnumber as an argument, but the cellnumber in getMove() is only a copy of cellnumber in main(). There is no reason to pass this argument. But don't forget do declare a local variable 'cellnumber' in getMove().

*On line 50 you want to check whether input was legal. I don't know what you do there, nor why it compiles, but you need while(!isLegal(cellnumber, playGrid)); note that isLegal requires playGrid so you'll have to pass it to getMove.

*getMove returns the correct value for cellnumber, but you never assign that value to cellnumber. line 19 should be cellnumber = getMove(playGrid);

*playGrid is an array of 8 chars, but you need 9. setGrid accesses 9'th element of 8 element array. This could cause errors.

*On line 49 you check whether cellnumber is in correct range. However note that 9 should be out of range.

*In C++, when passing array to function you don't need to specify its length. int getMove(char playGrid[])
Thanks alot! That worked perfectly.
Would you say that the code is readable - I know it's not the most efficient bit of programming in the world - still I did try to make it easy to follow.
for me it was readable enough :) try to add comments on what stuff does though.. In here it's obvious, but when the code gets more nested loops etc, it'll become harder to understand :)

And instead of using system("pause") try using something like std::cin.get. That's quiker and it's better (see: http://cplusplus.com/forum/articles/11153/ for more info)

Cheers!
:)
I don't see whats wrong with paper.
Topic archived. No new replies allowed.