Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Please correct my code :/

+3 votes
asked Jun 8, 2023 by Eidnoxon (5,140 points)
I tried to make a tic tac toe game in C++:

https://onlinegdb.com/TjYG-w5EH

(Don't ask me what did I wrote  I don't know either)

It's buggy. Please someone send me a corrected version of my code, and explain why is that specific line an error.

1 Answer

+1 vote
answered Jun 8, 2023 by Peter Minarik (86,110 points)
edited Jun 8, 2023 by Peter Minarik

For a start, fix the warnings:

main.cpp: In function ‘int computerMove(char*, char, char)’:
main.cpp:149:1: warning: control reaches end of non-void function [-Wreturn-type]
  149 | }
      | ^
main.cpp: In function ‘int CheckWinner(char*, char, char, bool)’:
main.cpp:191:1: warning: control reaches end of non-void function [-Wreturn-type]
  191 | }
      | ^

The warnings say that you have functions (computerMove and CheckWinner), where they are supposed to return a value, but the return value is never set.

This is less of an issue for computerMove, as you never read the return value. Change that function to void instead of int return type.

The problem is more dramatic for CheckWinner as it's supposed to indicate who won the game. You're even reading and using the value returned by this function, but you never really set what this function should return.

Fix these first and see how you go then (I don't have more time right now, I'll check on this question later).

One more thing: your table is not properly drawn, the middle and right columns are one to the left, see below:

    |    |    
  . |.   |.
    |    |    
____|____|_____
    |    |    
  . |.   |.
    |    |    
____|____|_____
    |    |    
  . |.   |.
    |    |    
    |    |     

And one more :). Your functions are super rigid, not flexible at all. You should make use of indirection, indexing, variables and so on to write flexible code and not copy-paste things around with minimal changes.

For instance, the PlayerMove could be written as simply as this:

void PlayerMove(char * spaces, char player, char computer, char empty)
{
    while (false)
    {
        int where = player;
        std::cout << "Where do you want to move? (1-9) " << std::endl;
        std::cin >> where;
        switch (spaces[where])
        {
            case empty: spaces[where] = player; return; // quit the function and the loop
            case player: std::cout << "This stop is alrady taken by you." << std::endl; break;
            case computer: std::cout << "This stop is alrady taken by the computer." << std::endl; break;
        }
    }
}

Good luck!

commented Jun 8, 2023 by Eidnoxon (5,140 points)
I have already corrected it. I realised that I have put `==` instead of `=` in my playerMove() and the computerMove() function. Thanks tho! :D
commented Jun 9, 2023 by Peter Minarik (86,110 points)
Yeah, that's a typical overlook to do a value assignment instead of an equality check. Happens to the bests of us.

Keep on learning! :)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...