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.