Password Guessing + Accessing command + writing to a file

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

Join Date: Jan 2009
Posts: 22
Reputation: jdm is an unknown quantity at this point 
Solved Threads: 0
jdm jdm is offline Offline
Newbie Poster

Password Guessing + Accessing command + writing to a file

 
0
  #1
May 29th, 2009
I working on part of a code for a much larger project. I understand how to write a program for a password in the simplest form, but I want to have it let the user guess the password up to 5 times and then not let them run a command.

For example if the user guess the password right then he can type debug and get the source code, but if he guess it wrong 5 times the app won't accept the command debug.

This code is in a rough state. What I'm having trouble is having it write out to the file. I know how to write out to a file, but this is driving me nuts. How the code is, it will keep on prompting for the password until 5 wrong guess or the correct password. So I wanted to have a way for the user can get out of that and do something else. So I coded an exit command, but it isn't working right for me. When they type exit I wanted to write the number of wrong guess so far to a file and then when they try again it will check the file and use that current number of guess for the count.

Any help or suggestion would be greatly appreciate.

Sincerely your;

jdm

P.S. If it isn't clear what I'm trying to do just ask.

Code:

  
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. void debug();
  7.  
  8. int main()
  9. {
  10. //open a file to read and write
  11. //declare file variables and name of file
  12. ifstream inData;
  13. ofstream outData;
  14. inData.open("guess.txt");
  15. outData.open("guess.txt");
  16.  
  17. string password;//store password input
  18. int number = 0;//store number of guess
  19. int num = 0;//store number of guess from file
  20.  
  21. inData >> num;//get number
  22. //making sure number of guess isn't 5
  23. if (num == 5)
  24. {
  25. cerr << "Access Denied" << endl;
  26. system("pause");
  27. exit (100);
  28. }
  29.  
  30. //A while loop that will prompt for the password as long as the number of guess isn't 5 and the right password isn't entered in
  31.  
  32. while (number !=5 && password != "access")
  33. {
  34. cout << "Enter password: ";
  35. cin >> password;
  36.  
  37. //trying to let the user exit the prompt, but keep track of the number of guesses, but it isn't writing out to the file right. I don't know why
  38. if (password == "exit")
  39. {
  40. outData << number;
  41.  
  42. system("pause");//through in thinking that the app was closing to early to write to the file.
  43.  
  44. exit(100);
  45. }
  46.  
  47. //launch the debug, but later it will allow the user the option of entering that command in.
  48. else if (password == "access")
  49. {
  50. debug();
  51. }
  52.  
  53. //increase number of guesses by 1 when wrong password is entered in
  54. else {
  55. number++;
  56. }
  57.  
  58. outData << number << endl
  59. cerr << number << endl;
  60. }
  61.  
  62. //close files
  63. inData.close();
  64. outData.close();
  65. system("pause");
  66. return 0;
  67. }
  68.  
  69. //just a test dummy
  70. void debug()
  71. {
  72. cerr << "Debug" << endl;
  73. cout << "Code Here" << endl;
  74. }

Thanks for the help.

jdm
Last edited by jdm; May 29th, 2009 at 2:01 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Password Guessing + Accessing command + writing to a file

 
0
  #2
May 29th, 2009
You should use break; instead of that exit command
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 22
Reputation: jdm is an unknown quantity at this point 
Solved Threads: 0
jdm jdm is offline Offline
Newbie Poster

Re: Password Guessing + Accessing command + writing to a file

 
0
  #3
May 29th, 2009
Originally Posted by tux4life View Post
You should use break; instead of that exit command
That worked great. Now I just need to work on it checking the file, but it kinda is hard to do in main. So I will change it to a function and try.

I have never seen break used that way before. The only way I have seen it is in a switch statement, but I only have 2 class (1 year) of c++ under my belt. Would you explain to me about what break does in this case. I would appreciate it.

What is the different between exit and break?

Thanks for the help.

jdm
Last edited by jdm; May 29th, 2009 at 2:46 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,673
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Password Guessing + Accessing command + writing to a file

 
0
  #4
May 29th, 2009
Putting the break (or exit( )) inside the if block is not a wholly satisfactory method. The conditions in loop ought to indicate when/why the loop ends - don't hide surprises inside the body.

How about a structure like:
  1. inData >> num;
  2.  
  3. if( num < 5 )
  4. {
  5. do
  6. {
  7. cin >> password;
  8. num++;
  9. if( password == "access" )
  10. {
  11. //whatever
  12. }
  13. else if( password == "exit" )
  14. {
  15. //whatever else
  16. }
  17. else
  18. {
  19. //oops, bad entry
  20. }
  21. }while( num < 5 && password != "access" && password != "exit );
  22. }
  23. if( num == 5 ) //either originally or through bad entries
  24. {
  25. //here's where you get nasty with the user
  26. }
  27.  

No exits from the program anywhere inside the if or loop blocks. Now you will always make it out to your cleanup code. As you originally wrote it, you could exit the program without getting to the file closing statements. Usually not a big problem, as they will be closed as program exits, but it's bad form.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 22
Reputation: jdm is an unknown quantity at this point 
Solved Threads: 0
jdm jdm is offline Offline
Newbie Poster

Re: Password Guessing + Accessing command + writing to a file

 
0
  #5
May 29th, 2009
Originally Posted by vmanes View Post
Putting the break (or exit( )) inside the if block is not a wholly satisfactory method. The conditions in loop ought to indicate when/why the loop ends - don't hide surprises inside the body.

How about a structure like:
  1. inData >> num;
  2.  
  3. if( num < 5 )
  4. {
  5. do
  6. {
  7. cin >> password;
  8. num++;
  9. if( password == "access" )
  10. {
  11. //whatever
  12. }
  13. else if( password == "exit" )
  14. {
  15. //whatever else
  16. }
  17. else
  18. {
  19. //oops, bad entry
  20. }
  21. }while( num < 5 && password != "access" && password != "exit );
  22. }
  23. if( num == 5 ) //either originally or through bad entries
  24. {
  25. //here's where you get nasty with the user
  26. }
  27.  

No exits from the program anywhere inside the if or loop blocks. Now you will always make it out to your cleanup code. As you originally wrote it, you could exit the program without getting to the file closing statements. Usually not a big problem, as they will be closed as program exits, but it's bad form.

Thanks for the help. I will give that a try and look through it and let you know how it goes.


I appreciate the help.

Sincerely yours;
jdm
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Password Guessing + Accessing command + writing to a file

 
0
  #6
May 29th, 2009
>What is the different between exit and break?

The exit command is used to exit the whole program and return a value to the operating system.
The break command is used to jump out a loop or to prevent running the instructions in the next case, if we're talking about the switch statement

Consider also the following links:
Last edited by tux4life; May 29th, 2009 at 12:07 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 22
Reputation: jdm is an unknown quantity at this point 
Solved Threads: 0
jdm jdm is offline Offline
Newbie Poster

Re: Password Guessing + Accessing command + writing to a file

 
0
  #7
May 29th, 2009
Originally Posted by tux4life View Post
>What is the different between exit and break?

The exit command is used to exit the whole program and return a value to the operating system.
The break command is used to jump out a loop or to prevent running the instructions in the next case, if we're talking about the switch statement

Consider also the following links:

Thanks for explaining and links. We take a look at them.

Thanks again for the help and advice.

Sincerely yours;
jdm
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Password Guessing + Accessing command + writing to a file

 
0
  #8
May 30th, 2009
>We take a look at them.
What do you mean: we ?

"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 45
Reputation: killdude69 is an unknown quantity at this point 
Solved Threads: 1
killdude69 killdude69 is offline Offline
Light Poster

Re: Password Guessing + Accessing command + writing to a file

 
0
  #9
May 30th, 2009
You shouldn't use a textfile, the user could just simply delete the text file or something. You could create the file in a hidden folder somewhere on the D: drive so the user could not find it, keeping your source code secure.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,400
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 113
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Password Guessing + Accessing command + writing to a file

 
0
  #10
May 30th, 2009
Originally Posted by killdude69 View Post
You shouldn't use a textfile, the user could just simply delete the text file or something. You could create the file in a hidden folder somewhere on the D: drive so the user could not find it, keeping your source code secure.
Nah, a text file is fine, so long as you ensure than any modifying of the file will not be accepted, and an encryption is used. Saving it somewhere else will just make it slightly harder to find, but once it has been found... then what?
I need pageviews! most fun profile ever :)
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