Little help with creating a C++ password function

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 23
Reputation: tedobg is an unknown quantity at this point 
Solved Threads: 0
tedobg tedobg is offline Offline
Newbie Poster

Little help with creating a C++ password function

 
0
  #1
Jan 28th, 2008
Firs of all hello guys.And then straight to the point Ok so my trouble is with creating a password function that will display '*' instead of normal chars.So here is my code:
  1.  
  2. string EnterPass()
  3. {
  4. string Pass="";
  5. char ch=getch();
  6. while (ch != '\r')
  7. {
  8. if(ch=='\b')
  9. cout<<'\b'<<' '<<'\b';
  10. else
  11. {
  12. cout << '*';
  13. Pass += ch;
  14. }
  15. ch=getch();
  16.  
  17. }
  18. return Pass;
  19. }
  20.  
  21. bool Access()
  22. {
  23. string password=EnterPass();
  24. if(password=="zerozone") return true;
  25. else return false;
  26. }
The first problem I came onto was that the program didn't handel backspaces and displayed them as '*' as well ,but i fixed that. Now the program displayes the backspaces right but it adds the to the string Pass and then adds the next letter I type resulting wrong password.
if I type "zerr" then "\b" and then "ozone" it results in "zerr\bozone". Please help .Any assistance will be appreciated.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,831
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Little help with creating a C++ password function

 
0
  #2
Jan 28th, 2008
How about changing this:

  1. if (ch=='\b')
  2. cout<<'\b'<<' '<<'\b';

to this:
  1. if (ch=='\b')
  2. {
  3. cout<<'\b'<<' '<<'\b';
  4. Pass.resize (Pass.length () - 1);
  5. }

You'd have to add code to make sure that you can't backspace on the empty string but it works otherwise as far as I have tested it.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 23
Reputation: tedobg is an unknown quantity at this point 
Solved Threads: 0
tedobg tedobg is offline Offline
Newbie Poster

Re: Little help with creating a C++ password function

 
0
  #3
Jan 28th, 2008
Thanks mate.You saved me hours.I am surprised that the answer is so simple .Thank you very much,it really works
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1
Reputation: trapkat20 is an unknown quantity at this point 
Solved Threads: 0
trapkat20 trapkat20 is offline Offline
Newbie Poster

Re: Little help with creating a C++ password function

 
0
  #4
Feb 10th, 2009
i had the same issue using the same code. im using dev C++ and if i use the backspace and go beyond the beginning of the entered password the program crashes. does anyone have a similiar problem
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC