C++ implementation of Tic-Tac-Toe game I have written this simple code for Tic Tac Toe.

#include &lt;iostream&gt;
#include &lt;vector&gt;

void draw_board(const std::vector&lt; std::vector&lt;char&gt; &gt;&amp; vec)
{
    std::cout &lt;&lt; "    0   1   2  \n";
    std::cout &lt;&lt; "  +---+---+---+\n";
    for (int i = 0; i &lt; vec.size(); i++)
    {
        std::cout &lt;&lt; i &lt;&lt; " " ;
        for (int j = 0; j &lt; vec[i].size(); j++)
        {
            std::cout &lt;&lt; "| " &lt;&lt; vec[i][j] &lt;&lt; " ";
        }
        std::cout &lt;&lt; "|";
        std::cout &lt;&lt; '\n';
        std::cout &lt;&lt; "  +---+---+---+\n";
    }
}

void enter(int row, int col, char ch, std::vector&lt; std::vector&lt;char&gt; &gt;&amp; vec)
{
    vec[row][col] = ch;
}

bool check(const std::vector&lt; std::vector&lt;char&gt; &gt;&amp; vec)
{
    //to check diagonals
    if ((vec[0][0] == 'X' &amp;&amp; vec[1][1] == 'X' &amp;&amp; vec[2][2] == 'X')
        || (vec[0][2] == 'X' &amp;&amp; vec[1][1] == 'X' &amp;&amp; vec[2][0] == 'X'))
        {
            std::cout &lt;&lt; "Player X won this game\n";
            return true;
        }
    else if ((vec[0][0] == 'O' &amp;&amp; vec[1][1] == 'O' &amp;&amp; vec[2][2] == 'O')
             || (vec[0][2] == 'O' &amp;&amp; vec[1][1] == 'O' &amp;&amp; vec[2][0] == 'O'))
             {
                std::cout &lt;&lt; "Player O won this game\n";
                return true;
             }

    //to check horizonatal and vertical
    for (int i = 0; i &lt; vec.size(); i++)
    {
        if ((vec[i][0] == 'X' &amp;&amp; vec[i][1] == 'X' &amp;&amp; vec[i][2] == 'X')
              || (vec[0][i] == 'X' &amp;&amp; vec[1][i] == 'X' &amp;&amp; vec[2][i] == 'X'))
              {
                  std::cout &lt;&lt; "Player X won this game\n";
                  return true;
              }
        else if((vec[i][0] == 'O' &amp;&amp; vec[i][1] == 'O' &amp;&amp; vec[i][2] == 'O')
              || (vec[0][i] == 'O' &amp;&amp; vec[1][i] == 'O' &amp;&amp; vec[2][i] == 'O'))
              {
                  std::cout &lt;&lt; "Player O won this game\n";
                  return true;
              }
    }
}

void start(std::vector&lt; std::vector&lt;char&gt; &gt;&amp; vec)
{
    int row, col, res = 0;
    char ch;

    std::vector&lt;int&gt; index = {0, 1, 2};

    for (int i = 0; i &lt; vec.size(); i++)
    {
        for (int j = 0; j &lt; vec[i].size(); j++)
        {
            vec[i][j] = ' ';
        }
    }

    for (int i = 0; i &lt; 9;)
    {
        if (i == 0 || i%2 == 0)
        {
            ch = 'X';
            std::cout &lt;&lt; "Chance to enter X\n";
        }
        else
        {
            ch = 'O';
            std::cout &lt;&lt; "Chance to enter O\n";
        }
        std::cout &lt;&lt; "Enter row number ";
        std::cin &gt;&gt; row;
        std::cout &lt;&lt; "Enter column number ";
        std::cin &gt;&gt; col;
        if (vec[row][col] == ' ' || vec[row][col] == ' ')
        {
            enter(row, col, ch, vec);
            draw_board(vec);
            if ( i &gt;= 2)
            {
                res = check(vec);
                if (res == 1)
                {
                    break;
                }
            }
            i++;
        }
        else
        {
            std::cerr &lt;&lt; "This position already contains a character\n";
        }

    }
    if (res == 0)
    {
        std::cout &lt;&lt; "This game draws\n";
    }
}

int main()
{
    std::vector&lt; std::vector&lt;char&gt; &gt; board(3, std::vector&lt;char&gt;(3));
    draw_board(board);
    start(board);
}
Check your inputs in enter()

You don't check that the user entered a valid vector index. For example, if I enter a 3 as the row and/or column number the program crashes. You should reject an input if the user enters an invalid row and/or column number (like you do if the user selects a space that is already taken).

All control paths of check() should return a value

You only ever return true in check(). If none of the check conditions are true then you should explicitly return false at the end of the function.

Use the correct type for indexing

You have for loops like for (int i = 0; i &lt; vec.size(); i++), which compare the signed integer i to the unsigned integer vec.size(). Use std::vector&lt; std::vector&lt;char&gt; &gt;::size_type.

Use a type alias / type definition for the std::vector&lt; std::vector&lt;char&gt; &gt; container

Use a type alias like using container = std::vector&lt; std::vector&lt;char&gt; &gt;; if your compiler supports it, otherwise use a type definition like typedef std::vector&lt; std::vector&lt;char&gt; &gt; container;. You can put it after the #include statements so you can use it for all your functions. This is useful for a couple of reasons:


It's a shorter name for the container. This comes in handy when referring to the container and/or its size_type, for example: container::size_type is shorter and easier to read than std::vector&lt; std::vector&lt;char&gt; &gt;::size_type.
It's easier to change the container. You may decide later to use a std::array instead, in which case you only need to modify the using statement rather than all instances of std::vector&lt; std::vector&lt;char&gt; &gt;.