首页 > 解决方案 > 如何在我的 C++ 程序中删除退格键

问题描述

__input create;__ 
//input is the class for saving username and password

char c = getch();
while(c!='\n'&&c!='\r'&&c!='\b') /*does my while loop have to be modified to 
check if the ASCII for backspace was entered*/  
{

    create.password += c;
    system("cls");

    cout << "Create an account" << endl << endl;

    cout << "Email: " << endl;

    cout << create.email << endl;


    cout << endl << "Username: " << endl;
    cout << create.name << endl;


    cout << endl << "Password: " << endl;
    cout << string(create.password.size(),'*');

//这些是用户名和密码的输入,它们都是字符串

    c = getch();
}

//PS 我是新手,所以任何建议都会有所帮助。

标签: visual-c++

解决方案


好吧,退格被认为是 achar因为它是char. 不是可打印的字符,而是控制字符。如果您查看 ASCII 表,您会发现它由数字表示,8就像'a' == 97.

因此,您可以检查c == 8您的循环并做任何您想做的事情,例如删除密码的最后一个字符。


推荐阅读