密码字段中显示额外的字母
Extra letter being displayed in Password Field
void createAccount(){
int i=0;
cout<<"nEnter new Username: ";
cin.ignore(80, 'n');
cin.getline(newUsername,20);
cout<<"nEnter new Password: ";
for(i=0;i<10,newPassword[i]!=8;i++){
newPassword[i]=getch(); //for taking a char. in array-'newPassword' at i'th place
if(newPassword[i]==13) //checking if user press's enter
break; //breaking the loop if enter is pressed
cout<<"*"; //as there is no char. on screen we print '*'
}
newPassword[i]=' '; //inserting null char. at the end
cout<<"n"<<newPassword;
}
在功能createAccount();
中,用户输入char newUsername[20]
和char newPassword[20]
。但是,要将密码显示为******,我实现了一种不同的输入newPassword
方式。但是,当我尝试显示newPassword
输出有一个额外的字母时,它神奇地出现在命令框中,而无需我输入任何内容。
输出
Enter new Username: anzam
Enter new Password: ****** //entered azeez but the first * is already there in command box without user inputting anything
Mazeez //displaying newPassword
如果有人能帮助我,我将不胜感激。
一个问题可能是你混合了conio
(getch
(和iostream
(cin
(,它们可能不同步。尝试在程序开头添加以下行:
ios_base::sync_with_stdio ();
另外,您读取密码直到看到13
,但是,如果我没记错的话,实际上在窗口中按 Enter 首先产生10
,然后13
,因此您可能需要检查两者作为停止条件。
i
在循环结束时递增。解决此问题的最简单方法是将password
初始化为零
char newPassword[20];
memset(newPassword, 0, 20);
for (i = 0; i < 10; )
{
int c = getch();
if (c == 13)
break;
//check if character is valid
if (c < ' ') continue;
if (c > '~') continue;
newPassword[i] = c;
cout << "*";
i++; //increment here
}
相关文章:
- 将结构字段的类型展开为可变模板参数
- 将位字段导出到数组
- 为了方便起见,我应该避免公开私有字段变量吗
- 当字段可以为null时,如何使用C++接口在Avro中写入数据
- 在java中读取c++字节的位字段
- 链接器找不到在虚拟类 c++ 中访问的静态字段的符号
- 私有字段对象与私有继承?
- 声明没有默认构造函数的字段
- C++内存模型和位字段的最大序列
- 声明为无效的变量或字段'...' Ardunio 编译器上的错误
- 如何在QByteArray中放置和检索位字段而不会感到痛苦?
- C++ win32 如何使密码字段可选并启用复制和粘贴?
- 如何通过UDP接收QByteArray并将其解析为位字段结构?
- 仅匹配集合中的某些字段
- 结构字段名称与 GDB 中的 STL 数组冲突
- Eclipse 编译器有没有办法将变量中的地址字段显示为整数值?
- 在行中显示单击TableView中的行时编辑字段
- 密码字段中显示额外的字母
- 凭据提供程序.显示一种进度条并禁用密码或PIN字段
- std::cout << 预测任意双精度显示的自动字段宽度