944,008 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6330
  • C++ RSS
Apr 24th, 2005
0

Need a little help with password code

Expand Post »
Well I got the password code right, I think.
I have a few problems
1) it wont close properly
2) Can someone point me in the right direction to find a tutorial that teach me how to only let the user have 34 tries before it close and also to encrypt the password to a file then decrypt it and compare it to another file that contain the correct password( I think that it use the fstream but I dont know the decrpytion, encryption, compare part )?
Thank you
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <conio.h>
  4. using namespace std;
  5.  
  6. int takingPass();
  7. int i;
  8. int pass1;
  9. char destination[13];
  10. int main()
  11. {
  12.  
  13. cout << "I'm going to kill your computer if you dont enter the right password!!!"<< endl;
  14.  
  15. for (i=0; i<13; i++) //user can input max 13 character
  16. {
  17. destination[i] = getch();
  18. cout << "*";
  19. if (destination[i]==13)
  20. {
  21. takingPass();
  22. }
  23. }
  24. }
  25.  
  26. int takingPass()
  27. {
  28. destination[i] = 0;
  29. pass1 = stricmp(destination,"lovebug"); //compare with name "lovebug"
  30. if (pass1 == 0) // case insensitive
  31. {
  32. cout <<"\nPASSWORD IS CORRECT \n";
  33. }
  34. else //if password is not correct
  35. {
  36. cin.clear(); // clear cin
  37.  
  38. cout <<"\nPASSWORD WAS INCORRECT\n";
  39. main(); //go to main to do until success
  40. }
  41. return 0; // exit program
  42.  
  43. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tetsu is offline Offline
16 posts
since Mar 2005
Apr 24th, 2005
0

Re: Need a little help with password code

>"I'm going to kill your computer if you dont enter the right password!!!"
Uh huh.

>main(); //go to main to do until success
It's illegal to call main recursively in C++.

>teach me how to only let the user have 34 tries before it close
C++ Syntax (Toggle Plain Text)
  1. // Pseudo-C++
  2. int n = 0;
  3.  
  4. while ( n < 34 ) {
  5. if ( is_right_password() )
  6. break;
  7. ++n;
  8. }
  9.  
  10. if ( n == 34 )
  11. exit ( EXIT_FAILURE );
  12.  
  13. // Do stuff for correct password
>encrypt the password to a file then decrypt it and compare it to
>another file that contain the correct password
Do you know how to read from a file? If not then it would be best to consult your C++ book (you do have one, right?). The encrypting and decrypting part is a little more difficult depending on your needs, but we'll get to that once you have a program that works with plain text. Those are easier to test for correctness.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 30th, 2005
0

Re: Need a little help with password code

>"I'm going to kill your computer if you dont enter the right password!!!"
--- well it made me laugh a little. I change some of the wording.

Okay I think I few some of the bugs but I dont know if the program is good.
I got it to save the password a file call out.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <conio.h>
  5. #include <stdio.h>
  6. using namespace std;
  7.  
  8. int takingPass();
  9. int o = 0;
  10. int i;
  11. int pass1;
  12. char ch[13];
  13. int wrong();
  14. int main()
  15. {
  16.  
  17.  
  18. ofstream out;
  19. out.open("c:\\out.txt");
  20.  
  21. cout << "Please enter the correct password."<< endl;
  22.  
  23. for (i=0; i<13; i++) //user can input max 13 character
  24. {
  25.  
  26. ch[i] = getch();
  27. cout << "*";
  28. out << ch[i];
  29.  
  30.  
  31.  
  32. if (ch[i]==13)
  33. {
  34. takingPass();
  35. cout << o;
  36. }
  37.  
  38. }
  39.  
  40. out.close();
  41. }
  42.  
  43. int takingPass()
  44. {
  45.  
  46. ch[i] = 0;
  47. o++;
  48. pass1 = stricmp(ch,"lovebug");
  49.  
  50. if (pass1 == 0)
  51. {
  52. cout <<"\nPASSWORD IS CORRECT \n";
  53. exit(1); // exit program if successful
  54. }
  55. else
  56. {
  57. system("cls");
  58. cout <<"PASSWORD WAS INCORRECT\nPLEASE RE-ENTER PASSWORD"<<endl;
  59. cout << "Number of re-tries: "<<o; // show o increasing
  60. cout<< endl;
  61. }
  62. if (o == 3)
  63. {
  64. cout << "YOU ARE A UNAUTHORIZE USER\n";
  65. exit(1); // exit program
  66. }
  67. main();
  68. return 0;
  69.  
  70. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tetsu is offline Offline
16 posts
since Mar 2005

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: Using Vectors and Arrays with fstream
Next Thread in C++ Forum Timeline: Help Need Urgently - C++ Gui





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


Follow us on Twitter


© 2011 DaniWeb® LLC