c++ - 密码字段中显示的额外字母

标签 c++ borland-c++

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

如果有人能帮助我,我将不胜感激。

最佳答案

一个问题可能是您将 conio (getch) 和 iostream (cin) 混合在一起,它们可能不同步。尝试在程序的开头添加这一行:

ios_base::sync_with_stdio ();

此外,您读取密码直到看到 13,但是,如果我没记错的话,实际上在 Windows 中按 enter 会先生成 10,然后生成 13,因此您可能希望将两者都检查为停止条件。

关于c++ - 密码字段中显示的额外字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33766923/

相关文章:

c++ - 通过在每个元素上调用 name() 将 Class 的 vector 转换为 decltype(Class::name()) 的 vector

c++ - 在 borland graphics c++ 中为球制作动画

c++ - 我在执行此功能时不断出错

c++ - 通过 HttpOpenRequest 发送 JSON 数据

c++ - Qt:恢复 QTextEdit 的几何形状失败

c++ - 不使用 "new"在 C++ 中创建对象

c++ - Borland C++ 5.02 cout 未显示在控制台窗口中

inline-assembly - 无法使用 Borland 执行命令 'tasm.exe'

C++ 模板元编程 : constexpr function

c++ - 如何将 txt 文件中的值插入 Qlist 并使用 QTablewideget