I am trying to read a pasword and while I read it , display ** .
cout << "\n Insert password : ";
loop1:
string passw1 = "";
char ch = _getch();
while (ch != 13) // enter = char 13
{
if (!isalnum(ch))
{
cout << "\n\n Invalid character! Please insert the password again! ";
goto loop1;
}
passw1.push_back(ch);
cout << "*";
ch = _getch();
}
If I press for example , BACKSPACE , or SPACE or something that is not alpha-numerical , everything goes as planned. The problem is when I press any F key , or DELETE , HOME , INSERT , END , PG UP , PG DOWN , when the program crashes . Could you help me avoid the crash ? I would like to show an error message if an invalid key is pressed , not to have my program crash.
Use the is alphanumeric function –
isalnum(char c )to check if the parameter c is either a decimal digitor an uppercase or a lowercase letter.
And then filter out characters less than 32 or higher than 122 like this :
if (c > 32 && c <122) { Get_Password(); }This MS Windows specific code below is not portable. For Linux/*NIX/BSD see this : Hide password input on terminal