How to delete an already written character in C++

2020-07-18 05:26发布

I'm trying to make a C++ program to read a password. I made the program to cout *, not the characters I write but my problem is when I want to delete character because they're wrong.
Example: My constant password is 12345
If I enter 1235 the program will show **** and I have to delete the last character. It's simple to remove it from the string but I want the last * to disappear from the console just as it happens when you introduce you windows password.
Is it possible? If it is, can someone explain how?

标签: c++
5条回答
对你真心纯属浪费
2楼-- · 2020-07-18 05:38

Sipmly write the '\b' character to stdout std::cout<<"\b". If you are using cpp or printf("\b") for pure C

查看更多
Emotional °昔
3楼-- · 2020-07-18 05:41
printf("\b ");

This statement surely works because after the cursor goes one character back and space given in above printf statement will overwrite the printed character on console output.

查看更多
Deceive 欺骗
4楼-- · 2020-07-18 05:42

When I am writting to console using putch function ( from conio.h ) to simulate backspace key simple

std::cout << '\b';

or

printf("\b ");

not works I have to write:

cout << '\b' << " " << '\b';

or

putch('\b');
putch(' ');
putch('\b');
查看更多
爷、活的狠高调
5楼-- · 2020-07-18 05:45

Outputting the backspace character '\b' may help to move the output point back.

Specifically, outputting the string "\b \b" should blank out the last character output.

查看更多
叛逆
6楼-- · 2020-07-18 05:53

Try backspace \b or erase the whole line and print it again.

查看更多
登录 后发表回答