944,138 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 18844
  • C++ RSS
Sep 23rd, 2005
0

password function (c++)

Expand Post »
hey guys, i'm using Borland 6 c++ in windows xp, now that we got that i have a problem with my program, i have recently created a password function in a programm that works so that when they try to type in their password it shows the * instead of the character for security measures (and just cus it is cool too lol) but the problem is that when someone makes a typo and wants to delete a character it still displays another *, is there a way that i can make the programm destroy one character when the backspace button is struck? here is my function so that you guys can look at it too.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <conio>
  3. #include <condefs.h>
  4. #pragma hdrstop
  5. #pragma argsused
  6.  
  7.  
  8. string EnterPassword(){
  9. string numAsString = "";
  10. char ch = getch();
  11. while (ch != '\r') {
  12. cout << '*';
  13. numAsString += ch;
  14. ch = getch();
  15. }
  16. return numAsString;
  17. }
  18.  
  19. void passfunction();
  20.  
  21.  
  22.  
  23. int main(int argc, char **argv)
  24. {
  25. passfunction();
  26. return 0;
  27. }
  28.  
  29.  
  30.  
  31. void passfunction()
  32. {
  33.  
  34. cout << endl;
  35. cout << "password:";
  36.  
  37. string password = EnterPassword();
  38.  
  39. if( passowrd == "rbrown" ){
  40. cout << "Hello Mr. Brown, Welcome back" << endl;
  41. system("PAUSE");
  42. leave = true;
  43. return;
  44. }
  45.  
  46. else if (password != "rbrown"){
  47. cout << "incorrect password, please try again." << endl;
  48. system("PAUSE");
  49. leave = false;
  50. passfunction (leave);
  51. }
  52.  
  53. cout << password << endl;
  54. system("PAUSE");
  55. return;
  56.  
  57. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
evilsilver is offline Offline
84 posts
since Feb 2005
Sep 23rd, 2005
0

Re: password function (c++)

Output a backspace ('\b') when on is input? And back up the string pointer and such?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 30th, 2007
0

Re: password function (c++)

Click to Expand / Collapse  Quote originally posted by evilsilver ...
hey guys, i'm using Borland 6 c++ in windows xp, now that we got that i have a problem with my program, i have recently created a password function in a programm that works so that when they try to type in their password it shows the * instead of the character for security measures (and just cus it is cool too lol) but the problem is that when someone makes a typo and wants to delete a character it still displays another *, is there a way that i can make the programm destroy one character when the backspace button is struck? here is my function so that you guys can look at it too.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <conio>
  3. #include <condefs.h>
  4. #pragma hdrstop
  5. #pragma argsused
  6.  
  7.  
  8. string EnterPassword(){
  9. string numAsString = "";
  10. char ch = getch();
  11. while (ch != '\r') {
  12. cout << '*';
  13. numAsString += ch;
  14. ch = getch();
  15. }
  16. return numAsString;
  17. }
  18.  
  19. void passfunction();
  20.  
  21.  
  22.  
  23. int main(int argc, char **argv)
  24. {
  25. passfunction();
  26. return 0;
  27. }
  28.  
  29.  
  30.  
  31. void passfunction()
  32. {
  33.  
  34. cout << endl;
  35. cout << "password:";
  36.  
  37. string password = EnterPassword();
  38.  
  39. if( passowrd == "rbrown" ){
  40. cout << "Hello Mr. Brown, Welcome back" << endl;
  41. system("PAUSE");
  42. leave = true;
  43. return;
  44. }
  45.  
  46. else if (password != "rbrown"){
  47. cout << "incorrect password, please try again." << endl;
  48. system("PAUSE");
  49. leave = false;
  50. passfunction (leave);
  51. }
  52.  
  53. cout << password << endl;
  54. system("PAUSE");
  55. return;
  56.  
  57. }
C++ Syntax (Toggle Plain Text)
  1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <iostream.h>
  6. //***************************************************************************//
  7. //************Program for entering password of max 8 characters**************/
  8. //************Press backspace if you make a mistake in the middle************//
  9. //************Press Enter once you are done**********************************//
  10. void main()
  11. {
  12. clrscr();
  13. int i=0,j;
  14. char ch,temp[8],arr[8],*password;
  15. strcpy(arr,"");
  16. while(i<9)
  17. {
  18. ch=getch();
  19. if(i==8&&(ch==13))
  20. break;
  21. if(ch==8)
  22. {
  23. if(i!=0)
  24. i--;
  25. }
  26. else
  27. if(ch==13&&i<8)
  28. {
  29. temp[i]='\0';
  30. break;
  31. }
  32. else
  33. if(i<8)
  34. {
  35. temp[i]=ch;
  36. arr[i]='*';
  37. i++;
  38. }
  39. clrscr();
  40. gotoxy(1,1);
  41. for(j=0;j<i;j++)
  42. cout<<arr[j];
  43. }
  44. password=new char[i];
  45. for(j=0;j<i;j++)
  46. password[j]=temp[j];
  47. password[j]='\0';
  48. cout<<"\nThe Password you typed is :";puts(password);
  49. getch();
  50. }

I just wrote this small program.I know there may be 100 better ways to write it but this was the first program I wrote for handling passwords.You can always use the getpass() function but there is a catch with that.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crazylunatic is offline Offline
13 posts
since Mar 2007
Mar 30th, 2007
0

Re: password function (c++)

Um... hello? This thread is like 2 years old, and plus your code isn't the finest quality... (like using void main and outdated headers)
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 31st, 2007
0

Re: password function (c++)

Um... hello? This thread is like 2 years old, and plus your code isn't the finest quality... (like using void main and outdated headers)
hmm..yes..That was before I joined this forum.Actually that was just my first post.I stumbled upon that by mistake and didnt see the date..And yeah I was really ignorant of the fact that you should never use void main().And the reason very obvious.."My teachers never told me it was wrong"..Anyways thats not an excuse..And about the outdated header..As I said that was written a long time ago on a now outdated Borland C++ 3.0..So its all my mistake..
Last edited by crazylunatic; Mar 31st, 2007 at 12:20 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crazylunatic is offline Offline
13 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: database connectivity in c++
Next Thread in C++ Forum Timeline: Help me understand the syntax, Urgent





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC