I am trying to make a tic tac toe game with c++ console, and for a player vs. computer i am just going to make the computer place an 'O' in random positions. using a char array, each char in the array is a different area on the tic tac toe board. My problem is that sometimes the rand() function does not work, or just sits there. How do i fix it? (code is unfinished btw)
EDIT: Well, I say must, but it doesn't have to be.
It's just that rand() produces a random-looking sequence of numbers based on a special number called a seed. Without giving a random seed the sequence is always the same...
A good seed is what's returned by time(), as the linked article suggests.
What does 'sometimes rand() does not work or just sits there' mean?
Are you upset with what rand() returns? It's actually quite random... if you're unhappy with a particular sequence it spits out at any time there's not much anyone can do to help you, unless you can describe exactly what kind of sequence you want.
Well, i want it to give me a number between 0 and 9, including 0 and 9. it works sometimes and updates the game board, but sometimes it doesnt do anything and just lets the user use his turn again.
Then it sounds like your issue lies with the input stream.
The input stream will only pause execution and ask for input when there's nothing in the current buffer, if there are still other characters left over from when you last extracted input it will just use that instead.
On your OS and with your compiler it's quite possible using cin.sync() can be used to clear the buffer and give you the kind of style you want... try adding cin.sync() before you read input every time?