| | |
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:
Solved Threads: 0
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:
Thanks for the help.
jdm
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:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; void debug(); int main() { //open a file to read and write //declare file variables and name of file ifstream inData; ofstream outData; inData.open("guess.txt"); outData.open("guess.txt"); string password;//store password input int number = 0;//store number of guess int num = 0;//store number of guess from file inData >> num;//get number //making sure number of guess isn't 5 if (num == 5) { cerr << "Access Denied" << endl; system("pause"); exit (100); } //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 while (number !=5 && password != "access") { cout << "Enter password: "; cin >> password; //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 if (password == "exit") { outData << number; system("pause");//through in thinking that the app was closing to early to write to the file. exit(100); } //launch the debug, but later it will allow the user the option of entering that command in. else if (password == "access") { debug(); } //increase number of guesses by 1 when wrong password is entered in else { number++; } outData << number << endl cerr << number << endl; } //close files inData.close(); outData.close(); system("pause"); return 0; } //just a test dummy void debug() { cerr << "Debug" << endl; cout << "Code Here" << endl; }
Thanks for the help.
jdm
Last edited by jdm; May 29th, 2009 at 2:01 am.
•
•
Join Date: Jan 2009
Posts: 22
Reputation:
Solved Threads: 0
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
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.
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:
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.
How about a structure like:
C++ Syntax (Toggle Plain Text)
inData >> num; if( num < 5 ) { do { cin >> password; num++; if( password == "access" ) { //whatever } else if( password == "exit" ) { //whatever else } else { //oops, bad entry } }while( num < 5 && password != "access" && password != "exit ); } if( num == 5 ) //either originally or through bad entries { //here's where you get nasty with the user }
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Jan 2009
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
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:
C++ Syntax (Toggle Plain Text)
inData >> num; if( num < 5 ) { do { cin >> password; num++; if( password == "access" ) { //whatever } else if( password == "exit" ) { //whatever else } else { //oops, bad entry } }while( num < 5 && password != "access" && password != "exit ); } if( num == 5 ) //either originally or through bad entries { //here's where you get nasty with the user }
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
>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:
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."
•
•
Join Date: Jan 2009
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
>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
•
•
Join Date: Mar 2008
Posts: 1,400
Reputation:
Solved Threads: 113
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 :)
![]() |
Similar Threads
- Writing to file (Java)
- Writing to File in Client/Server app (Java)
- writing to file (Python)
- writing to a file (C++)
- _popen not allowing writing to process (C++)
- Reading, Writing and Creating a File (C)
- DOS command for password protecting Files and Folders (IT Professionals' Lounge)
- Writing in a file (C++)
- writing to a file (Java)
Other Threads in the C++ Forum
- Previous Thread: creating a .txt file in c++
- Next Thread: System Resource Manipulating Script.
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






