0

I'm making a classic tic-tac-toe type game but with the board size defined by the user.

When trying to print the board (with asterisks representing open spaces) after the user specifies a size for the board, I'm only getting what seem to be garbage values.

#include <iostream>
#include <string>
using namespace std;

const int BOARD_MAX = 10;
const int BOARD_MIN = 3;

bool sizecheck(int);
void displayBoard(char [][BOARD_MAX], int);

int main(void)
{
    int boardsize, i, j;
    char grid[BOARD_MAX][BOARD_MAX];

    cout << "Please enter the size of the board (" << BOARD_MIN << " - " << BOARD_MAX << "): ";
    cin >> boardsize;

    if ((sizecheck(boardsize)) == false)
    {
        do
        {
            cout << "Must be a number between " << BOARD_MIN << " and " << BOARD_MAX << ". Try again." << endl;
            cout << "Please enter the size of the board (" << BOARD_MIN << " - " << BOARD_MAX << "): ";
            cin >> boardsize;

            sizecheck(boardsize);
        }

        while ((sizecheck(boardsize)) == false);
    }

    for (i = 0; i < boardsize; i++)
    {
        for (j = 0; i < boardsize; i++)
        {
            grid[i][j] = '*';
        }
    }

    displayBoard(grid, boardsize);

    return 0;
}

bool sizecheck(int fboardsize)
{
    if ((fboardsize > BOARD_MAX) || (fboardsize < BOARD_MIN))
    {
        return false;
    }

    else
    {
        return true;
    }
}


void displayBoard(char fgrid[][BOARD_MAX], int fboardsize)
{
    int i, j;

    for (i = 0; i < (fboardsize); i++)
    {
        for (j = 0; j < (fboardsize); j++)
        {
            cout << fgrid[i][j];
        }

        cout << endl;
    }
}

Output:

out http://puu.sh/6IfHU.png

The program is obviously a work in progress; I'm just having trouble with the printing of the array after using a function. If i try to print without the function, it works fine. I'm having a hard time figuring out what's going wrong.

Sorry for the super basic question; I'm very new to C++ and programming in general and am quite stumped.

Thanks!

2 Answers 2

1

check you grid init cycle...

for (i = 0; i < boardsize; i++)
{
    for (j = 0; i < boardsize; i++)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answering my question, and sorry for wasting your time! It slipped my notice at least 45 times. :)
happens :) take a look at c++ and stuff like for_each, it helps to reduce potential mistakes.
0

There is a typo in your code where you initialize the board: It should read

for (i = 0; i < boardsize; i++)
{
    for (j = 0; j < boardsize; j++)
    {
        grid[i][j] = '*';
    }
}

1 Comment

Oh lord, I feel silly! I guess sometimes it takes someone else to look at your code to point out dumb mistakes. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.