如何使用CLion在C ++中显示输入密码的星号

时间:2017-01-14 16:19:14

标签: c++ windows passwords codeblocks clion

在线密码程序有很多示例代码,用星号隐藏输入。当我使用CodeBlocks IDE编译它们时,这些程序通过在每次输入字母时输出*来工作。

Enter Password  : ******
You entered : iiiiii
Process returned 0 (0x0)   execution time : 6.860 s
Press any key to continue.

但是,当我使用CLion IDE时,我可以看到我输入的字母:

Enter Password  :iiiiii
 ******
You entered : iiiiii
Process finished with exit code 0

有人可以解释为什么两个IDE之间存在这种差异吗?

我正在使用的代码(我在网上找到)是:

#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std; //needed for cout and etc

int main()
{
    START:
    system("cls");
    cout<<"\nEnter Password  : ";
    char pass[32];//to store password.
    int i = 0;
    char a;//a Temp char
    for(i=0;;)//infinite loop
    {
        a=getch();//stores char typed in a
        if((a>='a'&&a<='z')||(a>='A'&&a<='Z')||(a>='0'&&a<='9'))
            //check if a is numeric or alphabet
        {
            pass[i]=a;//stores a in pass
            ++i;
            cout<<"*";
        }
        if(a=='\b'&&i>=1)//if user typed backspace
            //i should be greater than 1.
        {
            cout<<"\b \b";//rub the character behind the cursor.
            --i;
        }
        if(a=='\r')//if enter is pressed
        {
            pass[i]='\0';//null means end of string.
            break;//break the loop
        }
    }
    cout<<"\nYou entered : "<<pass;
    //here we can even check for minimum digits needed
    if(i<=5)
    {
        cout<<"\nMinimum 6 digits needed.\nEnter Again";
        getch();//It was not pausing :p
        goto START;
    }
    return 0;
}
//Lets check for errors.
//You can even put file system.

我知道有很多类似的问题,但是,没有一个问题可以解释为什么在使用CLion IDE时它无效。

6 个答案:

答案 0 :(得分:1)

也许

#include <conio.h>

int main()
{
    char s[10] = { 0 };
    int i;
    for (i = 0; i < 10;i++) {
        s[i] = _getch(); _putch('*');
        if (s[i] == 13) break;
    };
    printf("\nYour pass is %s", s);
    getchar();
    return 0;
}

答案 1 :(得分:1)

我知道这个线程有点旧,但是使用我的实现。它仅接受有效输入(数字和字母可以轻松更改),并且支持退格

#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int main()
{
    string password = "";
    while (!GetAsyncKeyState(VK_RETURN) & 1)
    {
        for (int i = 0x30; i < 0x5A; i++)
        {
            if (GetAsyncKeyState(i) & 1)
            {
                if (i >= 0x41 && i <= 0x5A && ((GetKeyState(VK_CAPITAL) & 1) == 0 || GetAsyncKeyState(VK_SHIFT) & 1))
                    password += ((char)i + 32);
                else
                    password += (char)i;

                cout << "*";
                Sleep(50);
            }
            else if (GetAsyncKeyState(VK_BACK) & 1)
            {
                password.erase(password.size() - 1);
                system("cls");
                for (int i = 0; i < password.size(); i++)
                {
                    cout << '*';
                }
                Sleep(50);
            }
        }
    }
    cout << password;
    Sleep(10000);
}

答案 2 :(得分:1)

对Stilet代码进行了一些增强

#include <conio.h>
#include <iostream>
int main()
{
    std::cout << "Please enter login password:";
    char s[10] = { 0 };
    int i;
    for (i = 0; i < 10;i++) {
        s[i] = _getch();
        if (s[i] == 13) {
            break;
        }
        else {
            _putch('*');
        }
    };
    printf("\nYour pass is %s", s);
    getchar();
    return 0;
}

答案 3 :(得分:1)

尝试一下。它将起作用。

#include<iostream>
#include<conio.h>
using namespace std;
int main() {
    char pin[100];
    int k=0;
    cout<<"Enter  password : ";
    while(pin[k-1]!='\r') {
        pin[k]=getch();
        if(pin[k-1]!='\r') {
            cout<<"*";
        }
        k++;
    }
    pin[k-1]='\0';
    cout<<"\nYou entered : "<<pin<<endl;
}

答案 4 :(得分:0)

用于保护密码的示例程序,防止有人在您键入密码时偷看它...希望这可能对您有用....任何建议都会受到赞赏。

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void pass();  // function declaration 
void main()
 {
       clrscr();
       cout<<"enter password : ";
       pass();  // function call
       getch();

 }
void pass();
  {
    int i,x;
    char ch='/0',password[]="example",match[20];
    for(i=0;i>=0;)
     {
       ch=getch();

      if(ch!=8&&ch!=13)
        {
         cout<<"*";
         match[i]=ch;
         i++;
        }
      else if (ch==8) // if backspace is presssed
       {
         cout<<"\b \b"; // moves cursor to the left print <space> again move cursor to left
         i--;
       }
      else if(ch==13)
    {
         match[i]='\0'; // if enter is pressed, last character in match[] becomes null
        break;         // for end of string
    }
    else
    {
         break;
    }
  }
  if(strcmp(match,password)==0)// comparing two strings.. if equal returns 0
  {
   cout<<endl<<"password correct";
  }
  else
  {
   cout<<endl<<"wrong password"<<endl;
   puts(match);
  }
}

答案 5 :(得分:-1)

#include <iostream>
#include <string>
#include <conio.h>
#include <stdlib.h>

using namespace std;

int main()
{
    string password,P;
    char p;
    cout<<"Enter Password: ";
    p=_getch();
    while(p!=13)
    {
        if(p==8)
        {
            P.resize(P.length()-1);
            cout<<P;
            password.resize(password.length()-1);
        }
        else {
            P=P+"*";
            cout<<P;
            password.push_back(p);
        }
        p=_getch();
        system("cls");
        cout<<"Enter Password: ";
    }
    cout<<endl<<password<<endl;
}