windows - Sådan vises asterisk for input adgangskode i C ++ ved hjælp af CLion

Indlæg af Hanne Mølgaard Plasc

Problem



Der er en masse prøvekode online for et kodeordsprogram, der gemmer indtastningen med en stjerne. Disse programmer fungerer, når jeg kompilerer dem med min CodeBlocks IDE ved at udstede en * hver gang jeg skriver i et brev.


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


Men når jeg bruger CLion IDE, kan jeg se bogstaverne, jeg skriver:


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


Kan nogen forklare, hvorfor denne forskel eksisterer mellem de to IDE'er?


Koden, jeg bruger (jeg fandt den online) er:


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

int main()
{
    START:
    system("cls");
    cout<<"
Enter 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==''&&i>=1)//if user typed backspace
            //i should be greater than 1.
        {
            cout<<" ";//rub the character behind the cursor.
            --i;
        }
        if(a=='
')//if enter is pressed
        {
            pass[i]='';//null means end of string.
            break;//break the loop
        }
    }
    cout<<"
You entered : "<<pass;
    //here we can even check for minimum digits needed
    if(i<=5)
    {
        cout<<"
Minimum 6 digits needed.
Enter Again";
        getch();//It was not pausing :p
        goto START;
    }
    return 0;
}
//Lets check for errors.
//You can even put file system.


Jeg ved, at der er mange spørgsmål svarende til denne, men ingen af ​​dem kunne forklare, hvorfor det ikke virker, når man bruger CLion IDE.

Bedste reference


måske


#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("
Your pass is \%s", s);
    getchar();
    return 0;
}

Andre referencer 1


Jeg ved, at denne tråd er lidt gammel, men brug min implementering. Det accepterer kun gyldig input (tal og bogstav kan nemt ændres), og det understøtter 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);
}

Andre referencer 2


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