【问题标题】:how to get a password input in c++ console application [duplicate]如何在 C++ 控制台应用程序中获取密码输入 [重复]
【发布时间】:2012-12-03 16:42:06
【问题描述】:

获取屏蔽密码输入的技术如下:

【问题讨论】:

    标签: c++ windows console passwords


    【解决方案1】:
    #include <iostream>
    #include<windows.h> // for system("pause")
    #include<conio.h>  //for getch()
    
    using namespace std;
    
    int main()
    {
        char x[10];
    
        cout<<"enter a password\n";
    
        for(int i=0; i<10;i++){
            x[i]=getch();
            cout<<"*";
    
            if(x[i]=='\r') //check if enter key is pressed
                break;
    
            else if(x[i]=='\b'){
    
                if(i==0)
                    cout<<"\b"<<" "<<"\b";
                else if(i>=1){
                    x[i-1]='\0';//make the previous byte null if backspase is pressed
                    i=i-2;
                    cout<<"\b"<<" "<<"\b\b"<<" "<<"\b";
                }
    
             }
        }
    
        cout<<endl<<"the password is  :"<<x<<endl;
        system("pause");
     }
    

    【讨论】:

    • getch() 已弃用。请改用_getch()。 (见msdn.microsoft.com/de-de/library/ms235446(v=vs.80).aspx
    • -1:要求是“不使用 winAPI 的 C++ 方法”。 getch()(或 _getch(),如果您愿意)是 Windows 函数,而不是 C++ 函数。
    • @Rob:对,_getch()是 Windows,而不是 C++。但是,独立于操作系统的解决方案的需求在哪里?这个问题甚至用 Windows 标记。
    • @Zane - 我编辑了标签以添加 windows 标签 - 因为解决方案显然是 Windows 的。它执行#include &lt;windows.h&gt;,#include &lt;conio.h&gt; 并使用getch/_getch。所有这些都是非标准的,显然不能在所有平台上工作。而且没有独立于平台的解决方案。
    猜你喜欢
    • 2017-03-14
    • 2014-01-22
    • 2011-04-09
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    相关资源
    最近更新 更多