Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Hiding Text Input in C++


ghost's Avatar
0 0

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:


ghost's Avatar
0 0

I'd cin the pass into an array of chars and for every char typed i'd cout a *…but that's just me.. maybe there's another more professional way to do it


ghost's Avatar
0 0

ya like i figured it would be something like that….but the problem is i dont know the correct syntax for it. BUT this helps none the less…this can narrow down my google searching….thank you


eXXon's Avatar
Member
0 0

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!"; } }


ghost's Avatar
0 0

you cant backspace huh thats pretty funny BUT THANK YOU!! this will help me alot!! well have a nice night.


eXXon's Avatar
Member
0 0

yea i guess.. im gonna try to write some more to the code and if i get the backspace to work then i'll post it in the code bank.. see ya


eXXon's Avatar
Member
0 0

yea i checked out your code before i posted mine here but they both have the same problem. if you hit backspace it takes it as another character and the string would be all mixed up in the case you made a mistake…


eXXon's Avatar
Member
0 0

ok so i finally finished writing the code for what you needed.. i just submitted it so i can have my first official code here in HBH. now we just have to wait for an admin to check it.


ghost's Avatar
0 0

awesome…thanks for all your help man…greatly appreciate it


eXXon's Avatar
Member
0 0

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 &lt; LENG; i++){
       password[i] = &#39;&#92;0&#39;;
   }
   
   while(kbinput != &#39;&#92;r&#39;){
          kbinput = getch();
          if(kbinput == &#39;&#92;b&#39; && password[0] == &#39;&#92;0&#39;){
			      loop = 0;
		  }
		  else if(kbinput == &#39;&#92;b&#39; && password[0] != &#39;&#92;0&#39;){
                  //cout &lt;&lt; &quot;&#92;b&quot;;
				  //cout &lt;&lt; &quot; &quot;;
				  //cout &lt;&lt; &quot;&#92;b&quot;; 
				  loop--;
				  password[loop] = &#39;&#92;0&#39;;
          }
          else if(isprint(kbinput) != 0 && loop &lt; LENG){
                  password[loop] = tolower(kbinput);
                  //cout &lt;&lt; &quot;*&quot;;
                  loop++;
          }
   }
   if(password == answer){
        cout &lt;&lt; &quot;&#92;nThe Password you entered is correct&quot;;
   }
   else{
        cout &lt;&lt; &quot;&#92;nWrong!&quot;;
   }
   /*   If you want to show the password
        for (int i = 0; i &lt; LENG; i++){
            cout &lt;&lt; password[i];
	    }
   */

}