password function (c++)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 84
Reputation: evilsilver is an unknown quantity at this point 
Solved Threads: 1
evilsilver's Avatar
evilsilver evilsilver is offline Offline
Junior Poster in Training

password function (c++)

 
0
  #1
Sep 23rd, 2005
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,414
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 248
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: password function (c++)

 
0
  #2
Sep 23rd, 2005
Output a backspace ('\b') when on is input? And back up the string pointer and such?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 11
Reputation: crazylunatic is an unknown quantity at this point 
Solved Threads: 0
crazylunatic crazylunatic is offline Offline
Newbie Poster

Re: password function (c++)

 
0
  #3
Mar 30th, 2007
Originally Posted by evilsilver View 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.

  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. }
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: password function (c++)

 
0
  #4
Mar 30th, 2007
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)
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 11
Reputation: crazylunatic is an unknown quantity at this point 
Solved Threads: 0
crazylunatic crazylunatic is offline Offline
Newbie Poster

Re: password function (c++)

 
0
  #5
Mar 31st, 2007
Originally Posted by joeprogrammer View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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