[C++] How to make hashed password input

in #coding6 years ago

Hello, in this post I will show yoy how to make a function

that asks for a password and when loading it covers the typed text with stars.


So, the first thing we do is write the skeleton of the function:


#include <iostream>
#include <conio.h>
using namespace std;
string password(){
}
int main(){
    password();
}

Ok, now lets write the code inside the password () function.


string password(){
    char key;
    int code;
    string pass = "";
    cout << "Enter password: ";
    do{
        key = getch();
        pass = pass + key;
        cout << "*";
        code = static_cast < int >( key );
    } while( code != 13 );
    cout << endl;
    return pass;
}

So there are three variables here.
NameTypeContent
KeyCharCurrently entered character
CodeIntThe code of the current character (ASCII)
PassStringThe entire loaded password (created from all entered characters)
Loading characters works as follows: The entire program should look like this:

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

using namespace std;

string password(){
    char key;
    int code;
    string pass = "";
    cout << "Enter password: ";
    do{
        key = getch();
        pass = pass + key;
        cout << "*";
        code = static_cast < int >( key );
    } while( code != 13 );
    cout << endl;
    return pass;
}

int main(){
    cout << "Your password is: " << password();
    return 0;
}

And the effect!

For more posts please add a comment and Follow my profile :) Thanks, MatiWasa