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.
Name | Type | Content |
---|---|---|
Key | Char | Currently entered character |
Code | Int | The code of the current character (ASCII) |
Pass | String | The entire loaded password (created from all entered characters) |
#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