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:

#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

You should use break; instead of that exit command :)

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

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:

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.

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:

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:

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

Thanks again for the help and advice.

Sincerely yours;
jdm

>We take a look at them.
What do you mean: we ?

:P

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.

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?

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?

But if you are using text files in the first place then you can expect them to be found. Setting it to read-only would make it to where you couldn't edit them either, as for an encryption, yes, that would work. But it doesn't seem completely right to protect a password with a password, because what password would you use to protect that password, and for that one, and so on.

But yeah, I don't think that an encryption would need any such additional maintenance.

Also, that is why you make it pretty much impossible to find, make a series of folders with numbers in the name as well as in the filename, so the user will not know what name to search for, it could look like:

D:
--->foldnm[1003388]
------->foldnm[97643]
----------->filenm[0773].txt
------->foldnm[97378]

commented: Good post. +10

Sorry, I meant I instead of we. I don't know why I typed that. The text file will be secure, but I'm not doing encryption. I'm making a folder and then renaming it so that it will act like the control panel when clicked upon.

That is my plan at least. Thanks for the help.

jdm

To make it a more secure method, you could do a binary write to the file with the time of day (in clock ticks) that the file was written, the number of guesses that occured, and a strange binary combination of the two (AND with time of day + binary shift once left then XOR with time of day). This way if anyone tampers with the file, this coded number will not be correct, and is sort of a 'keyed' encryption. This will require a hex viewer to read in the binary data, as well as a fair bit of reverse engineering to guess the algorithm! Happy coding!

Thanks for the help, but this program isn't going that far. It is meant to be a fun program, but certain areas like typing debug and getting the source code will have this. For the most part it is a fun app. Thanks for all of the help and suggestions. I appreciate it a lot.

Sincerely yours;

jdm

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.