我正在编写一个代码来接收密码输入。下面是我的代码...程序运行良好,但问题是除了数字和字母字符之外的其他键也被读取,例如删除、插入等。我可以知道如何避免它吗?咳咳……
string pw="";
char c=' ';
while(c != 13) //Loop until 'Enter' is pressed
{
c = _getch();
if(c==13)
break;
if(c==8)
{
if(pw.size()!=0) //delete only if there is input
{
cout<<"\b \b";
pw.erase(pw.size()-1);
}
}
if((c>47&&c<58)||(c>64&&c<91)||(c>96&&c<123)) //ASCii code for integer and alphabet
{
pw += c;
cout << "*";
}
}