【发布时间】:2015-11-17 21:01:19
【问题描述】:
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]='\0'; //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
如果有人可以帮助我,我将非常感激。
【问题讨论】:
-
循环条件
i<10, newPassword[i]!=8是可疑的。 -
我相信您看到的额外 * 是空白字符。如果您尝试检查 newPassword 数组,您得到的实际字符是什么?
-
我会将退格检查移到循环中,以便阅读清晰。 for(i=0;i
-
逗号应该改成
&&。见:Comma Operator in Conditon of Loop in C
标签: c++ borland-c++