Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

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.

2 Answers

Relevance
  • 7 years ago
    Favorite Answer

    For Windows, the <conio.h> header is still barely alive. These are low-level functions left over from MS-DOS. The most important ones for console input are:

    int _kbhit(); /* returns a nonzero (true) value if a key is ready to read */

    int _getch(); /* reads a key from the keyboard */

    int _ungetch(int ch); /* put a character back into the console input queue */

    You can find these and other <conio.h> functions documented at:

    http://msdn.microsoft.com/en-us/library/7x2hy4cx.a...

    The above names with leading underscore have been preferred since the early 1990s, but there are still a lot of books written by people who learned Microsoft C in the 1980s, and then stopped learning. You can still use getch(), kbihit() and ungetch(), I believe, but may have to endure warnings.

    These are easier to use than the official Windows console functions. Those defined in <windows.h> (along with a metric ton of other defintions) and this is what you want to learn if you want mouse input in a Windows console program. Doesn't sound like you need that for your program, but you can read about it here:

    http://msdn.microsoft.com/en-us/library/windows/de...

    If you are running on OSX, Linux or Unix, these won't work. One option there is to use a "curses" library. That's the original Unix name. The most popular open-source version is called "ncurses". Google around, but one introduction for C++ programmers is here:

    http://math.hws.edu/orr/s04/cpsc225/curses.html

    Note that it also has a "getch()" function, but the return values are different for "special keys".

    With all OS-specific code, you should consider writing your own class that provides services to your application and hides the OS details from the rest of your program. That style of coding lets you move from one OS to another with fewer problems.

    EDIT: /* facepalm */

    Somehow, I just didn't see that you are already using the Windows Console functions. The function you want to use is ReadConsole (for 1 input character) after first using the SetConsoleMode to turn off ENABLE_LINE_INPUT and ENABLE_ECHO_INPUT. You must use the console input handle returned by GetStdHandle( STD_INPUT_HANDLE ) for these functions, and not the output handle you are using in the code above.

  • Anonymous
    7 years ago

    Use the function getch

    http://www.programmingsimplified.com/c/conio.h/get...

    Since this is a tick tac toe game I don't think you want to get too advanced with it, but if you do...there are a lot more terminal manipulation functions in the curses library.

Still have questions? Get your answers by asking now.