0

I am creating a guessing game for two players. I first want a user to enter a number, which is intended to be secret. In order to keep it secret, I want to erase whatever the user has entered to cin last. I am trying to move the cursor back and insert a blank space where the user has entered a number. I am attempting to use cout << "\b" << "\b" << "\b" << " " as of right now, which you can see in my loop below.

here is my code so far:

do{ //initial do-while loop to begin game

    int secretNumber = 0; //initilized to a default value of 0 to make the compiler happy

    do {
        //prompt user for input
        cout << "Please choose a secret number between 1 and 10: ";
        //read in user input
        cin >> secretNumber; //store user input in variable
        cout << "\b" << "\b" << "\b" << " "; //<------- this is my attempt to clear the user input
        if(secretNumber <= 0 || secretNumber > 10){
            cout << "You have attempted to enter a number outside of the acceptable range." << endl;
        }   
    }while ((secretNumber <= 0 || secretNumber > 10)); //repeat do-while loop if user input is out of range

Currently this just prints another line, with a space as the first character, instead of going back to the previous line and replacing the user inputted integer with a " ".

Please do not give me any OS specific solutions, I need this to compile on both windows and linux.

  • There is no standard way to do it. Best bet is to find and use a portable library. The thing is standard library does not provide any facilities to deal with terminals. – luk32 Oct 12 '14 at 15:04
  • womp...womp...womp... –  Oct 12 '14 at 15:06
  • But what if I want to just print a bunch of control characters (delete to be precise) I have used this trick in Visual Basic before... –  Oct 12 '14 at 15:07
  • You have no guarantee how a given terminal will react to it. It's not standardized. – luk32 Oct 12 '14 at 15:08
  • Alright, well is it possible to print control characters in this fashion at all? I can compile this simple program in both environments to test it manually. –  Oct 12 '14 at 15:11
  • 2
    As already said, you can print control-characters, but there's no guarantee what happens on any given terminal or with any given editor/viewer. – Deduplicator Oct 12 '14 at 15:13

1 Answers1

2

If your terminal emulator (or the command shell you use on Windows) supports basic ANSI control characters, you could output a reverse line feed sequence followed by a kill line sequence. The reverse line feed is necessary because the user will have typed a carriage return in order to terminate the input, so the cursor is no longer on the same line. Those codes are "\033[F" and "\033[K", respectively. But there is no guarantee that this will work.

Historically, you could use the now-withdrawn Posix interface getpass to read a line without echoing. I don't believe that was implemented by Windows, and while it is still part of glibc, its use is discouraged. Unfortunately, there is no standard replacement.

Console-independent terminal control is available in Posix systems using the termios facilities; basically, you just need to turn off the ECHO flag. (See man termios for more details.) On Windows, there is a similar interface. However, it is very easy to get into trouble with these interfaces because you need to re-enable echoing even if the user kills the program with ctl-C (or suspends it with ctl-Z). Getting that right is tricky, and the naive solutions you'll find floating around on the internet typically don't.

Having said that, see Getting a password in C without using getpass (3)? for some possible solutions.

Community
  • 1
  • 1
rici
  • 234,347
  • 28
  • 237
  • 341
  • Thank you, this is a great answer. I will test it an hopefully it works. –  Oct 12 '14 at 15:26
  • On windows you can also `#include ` and do `_getch()` – Yam Marcovic Oct 12 '14 at 15:27
  • @YamMarcovic: If you are prepared to interpret backspace / delete / cursor movement (etc.) yourself, no? That's really a lot of work. – rici Oct 12 '14 at 15:38
  • One would think that even though the console is not standard across different OS, control characters would be. Or would typically be at least a little standardized. Oh well... –  Oct 12 '14 at 15:54