How to display asterisk for input password in C++

2019-06-09 17:44发布

There is a lot of sample code online for a password program that hides the input with an asterisk. These programs work when I compile them with my CodeBlocks IDE by outputting a * everytime I type in a letter.

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

However, when I use the CLion IDE, I can see the letters I type:

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

Can someone explain why this difference exists between the two IDEs?

The code that I am using (I found it online) is:

#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.

I know that there are a lot of questions similar to this one, however, none of them could explain why it is not working when using the CLion IDE.

4条回答
地球回转人心会变
2楼-- · 2019-06-09 17:50
#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;
}
查看更多
趁早两清
3楼-- · 2019-06-09 17:57

i know this thread is a little old but use my implementation. It only accepts valid input (numbers and letter can be easily changed) and it supports backspace

#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);
}
查看更多
霸刀☆藐视天下
4楼-- · 2019-06-09 17:58

sample program for password protection from someone peeking in display while you type it...hope this may work for you.... any suggestion will be appreciated..

#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);
  }
}
查看更多
Evening l夕情丶
5楼-- · 2019-06-09 18:10

Maybe

#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;
}
查看更多
登录 后发表回答