In C++, is there a way to limit the user to only one character of input?
I am programming a tic tac toe game for console using windows and visual 12, and would like the user input to break after the user hits any key once (whether it's a number, a letter, enter, space, etc).
here is my code:
void TicTacToe::moveX ()
{
char row [2];
char column [2];
int r, c;
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = {26, 1};
SetConsoleCursorPosition (screen, pos);
cout << "Player 1, pick a square.";
pos.X += 4; pos.Y +=11;
SetConsoleCursorPosition (screen, pos);
cout << "Row: ";
cin.getline (row, 2); //prevents user from hitting enter multiple times
r = (row[0] - 48); //convert char to int
pos.Y +=2;
SetConsoleCursorPosition (screen, pos);
cout << "Column: ";
cin.getline (column, 2); //prevents user from hitting enter multiple times
c = (column[0] - 48); //convert char to int
}
*NOTE* THIS IS NOT MY COMPLETE CODE!
Enter can't be hit multiple times with this, but the problem I run into is if the user just holds down a button, those numbers stay on the screen and mess everything up. How can I set it up so the user can not do that? Is there a way to only allow 1 single button entry? if not, is there a way to reset the entire screen to how it looked before each entry (board, prompt for row, prompt for column, etc.)?
I am new to programming, so please explain if your answer is somewhat complicated. Thank you in advance.