I am practicing my arrays, and I am trying to make a number/letter tic-tac-toe game. Here is my code:

#include<iostream>
using namespace std;

char board[3][3];
char letterNum;
void displayBoard();
void move();

void displayBoard()
{
     for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            board[0][0] = '1';
            board[0][1] = '2';
            board[0][2] = '3';
            board[0][3] = '4';
            board[1][0] = '5';
            board[1][1] = '6';
            board[1][2] = '7';
            board[1][3] = '8';
            board[2][0] = '9';
            board[2][1] = 'A';
            board[2][2] = 'B';
            cout << board[i][j];
            if (j < 2)
                  cout << '|';
            else
                  cout << endl;
        }
        
        if (i < 2)
              cout << "-----" << endl;
    }
}

int main()
{    
     displayBoard();
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
            board[i][j] = ' ';
    }
    move();
         
    
    system("PAUSE");
    return 0;
}

void move()
{
    cout << "Enter a number or letter that you want to move." << endl;
    cin >> letterNum;
    
    switch (letterNum)
    {
           case '1':
                board[0][0] = 'X';
           case '2':
                board[0][1] = 'X';
           case '3':
                board[0][2] = 'X';
           case '4':
                board[0][3] = 'X';
           case '5':
                board[1][0] = 'X';
           case '6':
                board[1][1] = 'X';
           case '7':
                board[1][2] = 'X';
           case '8':
                board[1][3] = 'X';
           case '9':
                board[2][0] = 'X';
           case 'A':
                board[2][1] = 'X';
           case 'B':
                board[2][2] = 'X';
    }
    displayBoard();
}

Thank you.

So what is your question?

Hehe my second day learning c++ and i can answer part of your post. Try not to use...

system("PAUSE");

because it messes the program up, use...

cin.get()

instead.

Oh, I am sorry. I forgot to post that. Here it is:
When I run the application, and type either a number or letter, the board comes up the same as when it first started. It is supposed to ask for a number or letter, then put an X wherever the number or letter was.

Using system("PAUSE"); does not make a difference.

I am practicing my arrays, and I am trying to make a number/letter tic-tac-toe game. Here is my code:

#include<iostream>
using namespace std;

char board[3][3];
char letterNum;
void displayBoard();
void move();

void displayBoard()
{
     for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            board[0][0] = '1';
            board[0][1] = '2';
            board[0][2] = '3';
            board[0][3] = '4';
            board[1][0] = '5';
            board[1][1] = '6';
            board[1][2] = '7';
            board[1][3] = '8';
            board[2][0] = '9';
            board[2][1] = 'A';
            board[2][2] = 'B';
            cout << board[i][j];
            if (j < 2)
                  cout << '|';
            else
                  cout << endl;
        }
        
        if (i < 2)
              cout << "-----" << endl;
    }
}

int main()
{    
     displayBoard();
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
            board[i][j] = ' ';
    }
    move();
         
    
    system("PAUSE");
    return 0;
}

void move()
{
    cout << "Enter a number or letter that you want to move." << endl;
    cin >> letterNum;
    
    switch (letterNum)
    {
           case '1':
                board[0][0] = 'X';
           case '2':
                board[0][1] = 'X';
           case '3':
                board[0][2] = 'X';
           case '4':
                board[0][3] = 'X';
           case '5':
                board[1][0] = 'X';
           case '6':
                board[1][1] = 'X';
           case '7':
                board[1][2] = 'X';
           case '8':
                board[1][3] = 'X';
           case '9':
                board[2][0] = 'X';
           case 'A':
                board[2][1] = 'X';
           case 'B':
                board[2][2] = 'X';
    }
    displayBoard();
}

Thank you.

As invisal mentioned, you need to tell us what the question/problem is. You should tell us what the goal of the program is, what its desired behavior is, what its actual behavior is, and where you are stuck. It'll help clarify things in your own head and will help us.

At first glance, you are overflowing your array boundaries in lines 18 and 22, and elsewhere. This is a 3 x 3 array, so you need to only use 0, 1, and 2 as array indexes. The compiler and the program at run-time may well NOT treat those lines as illegal due to the way it handles array offset calculations, but using 3 as an array index is a bad habit to get into, especially if you don't fully understand how array offsets work. Just what are the dimensions of this tic-tac-toe board? 3 x 3? You have options for '1' through '9' and 'A' and 'B', which is 11 boxes.

Yes it does, ask someone with experience of c++, anyway back to the topic, i think your problem is you havent added...

return 0;

to the end.

Using system("PAUSE"); does not make a difference.

It may not make a difference on the computer/operating system you are using, but it is not portable to all computers/operating systems so that is why there are many threads on Daniweb discouraging its use. cin.get() will always work with any computer/compiler.

switch (letterNum)
{
  case '1':
    board[0][0] = 'X';
[B]    break;[/B]

  case '2':
    board[0][1] = 'X';
[B]    break;[/B]

case '3':
.....

Remember to use break; for each case .

board[0][0] = '1';
board[0][1] = '2';
board[0][2] = '3';
board[0][3] = '4';
board[1][0] = '5';
board[1][1] = '6';
board[1][2] = '7';
board[1][3] = '8';
board[2][0] = '9';
board[2][1] = 'A';
board[2][2] = 'B';

You keep re-assign your array with the same data everytime the displayBoard run. Why don't you try to omit those lines out.

Could you explain what that means please? "omit lines out" i want to be able to understand these things :) Thanks

omit = delete = remove

Every time you call move() to change one of the values within the array you call displayBoard() immediately afterward. Within displayBoard() you write over any changes made in move() putting the array back to the default values, which is why it looks like it isn't changed within move().

Move lines 15-25 to main() before the first call to displayBoard() to get the effect I think you want.

Be sure to correct the index overwrite changes and add the break statements as previously recommended by other people as well.

Thank you, but it still wont work. Now I've got:

#include<iostream>
using namespace std;

char board[3][3];
char letterNum;
void displayBoard();
void move();

void displayBoard()
{
     for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            board[0][0] = '1';
            board[0][1] = '2';
            board[0][2] = '3';
            board[0][3] = '4';
            board[1][0] = '5';
            board[1][1] = '6';
            board[1][2] = '7';
            board[1][3] = '8';
            board[2][0] = '9';
            board[2][1] = 'A';
            board[2][2] = 'B';
            cout << board[i][j];
            if (j < 2)
                  cout << '|';
            else
                  cout << endl;
        }
        
        if (i < 2)
              cout << "-----" << endl;
    }
}

int main()
{    
     displayBoard();
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
            board[i][j] = ' ';
    }
    move();
         
    
    system("PAUSE");
    return 0;
}

void move()
{
    cout << "Enter a number or letter that you want to move." << endl;
    cin >> letterNum;
    
    switch (letterNum)
    {
           case '1':
                board[0][0] = 'X';
                break;
           case '2':
                board[0][1] = 'X';
                break;
           case '3':
                board[0][2] = 'X';
                break;
           case '4':
                board[0][3] = 'X';
                break;
           case '5':
                board[1][0] = 'X';
                break;
           case '6':
                board[1][1] = 'X';
                break;
           case '7':
                board[1][2] = 'X';
                break;
           case '8':
                board[1][3] = 'X';
                break;
           case '9':
                board[2][0] = 'X';
                break;
           case 'A':
                board[2][1] = 'X';
                break;
           case 'B':
                board[2][2] = 'X';
                break;
    }
    displayBoard();
    return;
}

And it still wont work. And what include would you use for cin.get();

Replace...

system("PAUSE")

With...

cin.get();

And instead of stopping the program, it will say press continue to continue, and it is also much safer for your program

Reread Lerner's and invisal's and my posts. As Lerner and invisal mentioned, you don't want this code in your displayBoard function:

board[0][0] = '1';
board[0][1] = '2';
board[0][2] = '3';
board[0][3] = '4';
board[1][0] = '5';
board[1][1] = '6';
board[1][2] = '7';
board[1][3] = '8';
board[2][0] = '9';
board[2][1] = 'A';
board[2][2] = 'B';

Display should do just that: display the board. Any ASSIGNMENT code such as the code done above should be done elsewhere. Lerner suggested doing it in main. You can do it there or you can have some function called InitializeBoard() which you call from main. I assume this code above is to occur only once?

Also, again from my earlier post, you are still using the array index 3. Don't do that. You also have 11 boxes instead of nine. Also, it seems to me that in tic-tac-toe, board[j] should be a blank/space or 'X' or 'O'. Why are you assigning 'A', 'B', and '1' through '9' to them? I think you are confusing what should displayed in board[j] and what the user enters. See back to your earlier thread where I posted the tic-tac-toe game for one way to do it.

You have to #include <iostream> to use cin.get() , which you have already done, so you can use it in your program without any other #include statements.

[EDIT]
I think I now understand the '1' through '9' and why you are displaying it. Those represent unused positions on the board where a player can move, and you are telling the player what to enter to move there? That makes sense, but I think you need to get rid of the 'A' and 'B'.
[/EDIT]

It had 2 errors now it has 1. Sorry im not good enough in c++ to find the error, this is my second day and im 15 lol

EDIT : Here it is with 1 error taken away... You forgot to add int main() at the top...

#include<iostream>
using namespace std;
int main()

char board[3][3];
char letterNum;
void displayBoard();
void move();

void displayBoard()
{
     for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            board[0][0] = '1';
            board[0][1] = '2';
            board[0][2] = '3';
            board[0][3] = '4';
            board[1][0] = '5';
            board[1][1] = '6';
            board[1][2] = '7';
            board[1][3] = '8';
            board[2][0] = '9';
            board[2][1] = 'A';
            board[2][2] = 'B';
            cout << board[i][j];
            if (j < 2)
                  cout << '|';
            else
                  cout << endl;
        }
        
        if (i < 2)
              cout << "-----" << endl;
    }
}

int main()
{    
     displayBoard();
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
            board[i][j] = ' ';
    }
    move();
         
    
    cin.get();

    return 0;
}

void move()
{
    cout << "Enter a number or letter that you want to move." << endl;
    cin >> letterNum;
    
    switch (letterNum)
    {
           case '1':
                board[0][0] = 'X';
                break;
           case '2':
                board[0][1] = 'X';
                break;
           case '3':
                board[0][2] = 'X';
                break;
           case '4':
                board[0][3] = 'X';
                break;
           case '5':
                board[1][0] = 'X';
                break;
           case '6':
                board[1][1] = 'X';
                break;
           case '7':
                board[1][2] = 'X';
                break;
           case '8':
                board[1][3] = 'X';
                break;
           case '9':
                board[2][0] = 'X';
                break;
           case 'A':
                board[2][1] = 'X';
                break;
           case 'B':
                board[2][2] = 'X';
                break;
    }
    displayBoard();
    return ;
}

Now we have to find the last error...

I got it from 2 errors to 1 then messed it up but trying to get rid of that 1 error.

EDIT : Here is it with 1 error taken away... You forgot to add int main() at the top...

Now we have to find the last error...

#include<iostream>
using namespace std;
int main()    // delete this line

char board[3][3];
char letterNum;
void displayBoard();
void move();

// code

int main()  // main is here.
{    
     // code
}

// code

Delete the line in red. You already have the main function below (see green text). Every program must have a main function. Therefore you do not have to declare it at the top as you had to with say, the move function. The compiler always knows to look for main since it must always be there. It DOES NOT know that it must look for move, which is why you had to declare move at the top.

Ah ok, whats the last error then?

Ah ok, whats the last error then?

There is no last error. Comment out or delete that first int main() line and the code you posted will compile and run without error. It won't run very well due to design flaws, but it will compile and it will run to completion. I just copied and pasted your code verbatim, deleted that line, and compiled and ran it in Dev C++ on Windows XP. What error are you getting and under what circumstances?

I had one error and then when i deleted that code i had 2 errors,

So i put the int main() back and now i have one error left, and this is what the error code says...

error C2144: syntax error : 'char' should be preceded by ';'

I had one error and then when i deleted that code i had 2 errors,

So i put the int main() back and now i have one error left, and this is what the error code says...

error C2144: syntax error : 'char' should be preceded by ';'

That error makes perfect sense because that's a real error. The first int main () shouldn't be there. What doesn't make sense to me are the two errors that you got when you deleted that first int main () . I didn't get those two errors after that line was deleted and I don't see why there would be two errors. What are the two errors? gangsta gama, is it compiling for you? And do you have any updated code or problems?

It is better if you post the exact code and the exact error messages in here.

#include<iostream>
using namespace std;
int main()

char board[3][3];
char letterNum;
void displayBoard();
void move();

void displayBoard()
{
     for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            board[0][0] = '1';
            board[0][1] = '2';
            board[0][2] = '3';
            board[0][3] = '4';
            board[1][0] = '5';
            board[1][1] = '6';
            board[1][2] = '7';
            board[1][3] = '8';
            board[2][0] = '9';
            board[2][1] = 'A';
            board[2][2] = 'B';
            cout << board[i][j];
            if (j < 2)
                  cout << '|';
            else
                  cout << endl;
        }
        
        if (i < 2)
              cout << "-----" << endl;
    }
}

int main()
{    
     displayBoard();
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
            board[i][j] = ' ';
    }
    move();
         
    
    cin.get();

    return 0;
}

void move()
{
    cout << "Enter a number or letter that you want to move." << endl;
    cin >> letterNum;
    
    switch (letterNum)
    {
           case '1':
                board[0][0] = 'X';
                break;
           case '2':
                board[0][1] = 'X';
                break;
           case '3':
                board[0][2] = 'X';
                break;
           case '4':
                board[0][3] = 'X';
                break;
           case '5':
                board[1][0] = 'X';
                break;
           case '6':
                board[1][1] = 'X';
                break;
           case '7':
                board[1][2] = 'X';
                break;
           case '8':
                board[1][3] = 'X';
                break;
           case '9':
                board[2][0] = 'X';
                break;
           case 'A':
                board[2][1] = 'X';
                break;
           case 'B':
                board[2][2] = 'X';
                break;
    }
    displayBoard();
    return ;
}

Error = error C2144: syntax error : 'char' should be preceded by ';'

And dont say "delete

int main()

because when i do delete it i get 1 more error.

using namespace std;
int main()[B];[/B]

char board[3][3];

You have forgotten to put the semi-colon to your main() function declaration. Anyway, I don't think it is neccessary to declare the main function at all. I suggest you to remove that line and then tell me what is the error message after you delete it.

#include<iostream>
using namespace std;
int main()  // <- This line simply just MUST 
            // NOT be here .. there is no way around it

I deleted it and got more errors.

I deleted it and got more errors.

What is your compiler and what are the errors?

I deleted it and got more errors.

Like I have told you. Posting those error messages after you delete that line and it would be better if you post the exact code after your delete that line. It is hard to talk thing blindly.

MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
C:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\Game\Debug\Game.exe : fatal error LNK1120: 1 unresolved externals

Did you choose "Console Project" or "Win32 Project"?

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.