This cannot be done initially on the terminal; you must do this in your control flow.
A small example
string text("Hello, World") cout << text; char x = getch(); while (x != '\n') { //loop breaks if you press enter if (x == 127 || x == 8) { //for backspace(127) and delete(8) keys cout << "\b \b"; //removes last character on the console text.erase(text.size() - 1); } else { cout << x; text.append(x); } x = getch(); }
"\b" is a non-destructive backspace. that is, it moves the cursor back, but does not erase. "\b \b" is a destructive backspace.
source share