简体   繁体   English

如何在 C++ 控制台中删除已写入的字符

[英]How to delete an already written character in C++ console

I'm trying to make a C++ program to read a password.我正在尝试制作一个 C++ 程序来读取密码。 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.我让程序 cout * ,而不是我写的字符,但我的问题是当我想删除字符时,因为它们是错误的。
Example: My constant password is 12345示例:我的固定密码是12345
If I enter 1235 the program will show **** and I have to delete the last character.如果我输入1235 ,程序将显示**** ,我必须删除最后一个字符。 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.从字符串中删除它很简单,但我希望最后一个*从控制台中消失,就像你介绍 Windows 密码时发生的那样。
Is it possible?是否可以? If it is, can someone explain how?如果是,有人可以解释一下吗?

Outputting the backspace character '\\b' may help to move the output point back. 输出退格符'\\ b'可能有助于将输出点移回。

Specifically, outputting the string "\\b \\b" should blank out the last character output. 具体来说,输出字符串“\\ b \\ b”应该删除最后一个字符输出。

printf("\b ");

这句话肯定有效,因为在光标返回一个字符后,上面printf语句中给出的空格将覆盖控制台输出上的打印字符。

When I am writting to console using putch function ( from conio.h ) to simulate backspace key simple 当我使用putch函数(从conio.h)写入控制台来模拟退格键简单时

std::cout << '\b';

or 要么

printf("\b ");

not works I have to write: 我不得不写作:

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

or 要么

putch('\b');
putch(' ');
putch('\b');

尝试退格\\ b或删除整行并再次打印。

Sipmly write the '\\b' character to stdout std::cout<<"\\b" . Sipmly将'\\b'字符写入stdout std::cout<<"\\b" If you are using cpp or printf("\\b") for pure C 如果您使用cpp或printf("\\b")表示纯C

I faced the same thing recently, using the \\b was** moving the cursor backward** but it was not deleting the char (or * ).我最近遇到了同样的事情,使用\\b是**向后移动光标**但它没有删除字符(或 * )。 I realize after that the next character you enter after \\b is gonna replace the (character to be deleted).我意识到在\\b之后输入的下一个字符将替换(要删除的字符)。 So, I came up with something like this所以,我想出了这样的事情

printf(“\b \b”);

using the space key after \\b will replace the character to be deleted and again using the \\b will take you one position back again (to the position where was incorrect character was, but this time the character will not be there) This way your output will look smooth.\\b之后使用space key将替换要删除的字符,再次使用\\b会将您带回一个位置(到不正确字符的位置,但这次字符将不在那里)这样您的输出看起来很流畅。 hope it will be helpful to you希望对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM