Hiding Text Input in C++
Okay just to throw this out im a bit of a n00b when it comes to C++ and i am Writing this login program. and i want to know how i would make what you type in for the password to be asterisks or anything i want…i am currently googling it and i thought i would throw this out maybe one of you 313375 know what the Syntax is or can point me to a Tut. website basically any help would be nice….hope to get many responses……:radio:
This would probably work.. the only problem is you cant erase what you typed.. meaning if you misspell something and try to go back and correct it with backspace or something the answer would still be wrong.. but hope it helps..
#include <iostream> #include <conio.h> #include <string> #define LENG 20
using namespace std;
main(){ cout << "passwd:"; char password[LENG], letter = '\0'; int loop = 0, len = 0; string answer = "hello"; for (int i = 0; i < LENG; i++){ password[i] = '\0'; } while(letter != '\r'){ letter = getch(); if(isprint( letter ) != 0 && loop < LENG){ password[loop] = tolower(letter); } loop++; if (loop <= LENG) len++; } if(password==answer){ cout << "\nThe Password you entered is correct"; } else{ cout << "\nWrong!"; } }
so i think theres a problem with the submit code so here you go.. hope that helps you out..
/*
- Hide the password input in C++
- if you want there to be *'s instead of blank
- you erase the commented lines inside the code
- by eXXon */
#include <iostream> #include <conio.h> #include <string> #define LENG 20
using namespace std;
main(){ cout << "Password:"; char password[LENG], kbinput = '\0'; int loop = 0; string answer = "password";
for (int i = 0; i < LENG; i++){
password[i] = '\0';
}
while(kbinput != '\r'){
kbinput = getch();
if(kbinput == '\b' && password[0] == '\0'){
loop = 0;
}
else if(kbinput == '\b' && password[0] != '\0'){
//cout << "\b";
//cout << " ";
//cout << "\b";
loop--;
password[loop] = '\0';
}
else if(isprint(kbinput) != 0 && loop < LENG){
password[loop] = tolower(kbinput);
//cout << "*";
loop++;
}
}
if(password == answer){
cout << "\nThe Password you entered is correct";
}
else{
cout << "\nWrong!";
}
/* If you want to show the password
for (int i = 0; i < LENG; i++){
cout << password[i];
}
*/
}