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

How to display asterisk for input password in C++ using CLion

网上有很多密码程序的示例代码,可以用星号隐藏输入。这些程序在我使用我的代码块 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]='[=12=]';//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.

我知道有很多与此类似的问题,但是,其中 none 可以解释为什么在使用 CLion IDE 时无法正常工作 IDE。

也许

#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;
}

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

#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);
}
#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;
}

密码保护示例程序,防止有人在您输入密码时偷看显示屏...希望这对您有用...任何建议将不胜感激..

#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]='[=11=]'; // 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);
  }
}

对 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;
}

试试这个。它会起作用。

#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]='[=10=]';
    cout<<"\nYou entered : "<<pin<<endl;
}