I'm working on a password program for fun. If the password is correct launch something. If password is wrong prompt again. After 5 wrong guess don't allow the user to try and guess again, but allow them to get to the regular parts of the code.

For example. If they type bofh it will launch a txt file, but if they type debug and get it correct, it will launch a txt file of the source code. After 5 guess they can only access bofh.

Is this possible.

Thanks for the help.

I know you can't give me the code, but what would be the best way of going about this?

Thanks for the help.

Oh, you want some clues? ;)

Use a loop to get the password from user, limiting the number of attempts. Keep some indicator of success.

Test the success indicator. If good, launch one text file. If not, launch the other.

Just what do you mean by "get to" or "launch" text? Display it from within this program or actually launch another program such as notepad, passing to it the particular file to open?

What I'm refering to is just part of a bigger program that I'm working on, but I will try and explain it the best I can and try and show the code too. I can't show the whole code that I'm working on right now. I think it would be too big for the site ;)

Here is just a thrown together program that doesn't relate to anything, but the question.

#include <iostream>
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;

void bofh();
void test();

int main()
{
    string x;
    cerr << "Enter a command: ";

    getline(cin,x);

    if (x == "bofh")
       bofh();
    else if (x == "test")
         test();
    else if(x == "exit")
         exit(1);
   // system("pause");
    return 0;
}

void bofh()
{
     string x;
     int count =0;
     while(count < 5 && x != "access")
     {
     cerr << "Password: ";
     getline(cin,x);
     if (x == "access")
     system("\"C:\\bofh1.txt\"");
     else {
          cout << "wrong password" << endl;
          system("pause");
          system("cls");
          count++;
          } 
          }
          main();
}

void test()
{
     system("\"C:\\bofh2.txt\"");
}

I know this isn't the best code, but it might help you to understand what I mean. I just threw it together in a minute. So sorry if your offended by the code.

In the program that I'm working on the user can enter in commands and the program will keep on going until they enter exit, but for some of the commands they have to enter a password. Right now I have it that if the password is wrong just take them back to main, but a friend of mine suggested that if they guess the password 5 times and get it wrong 5 times then all they can do is just the commands that don't require a password.

Does that make sense?

Note: Just update this post with a better example of the code.

In this code the user can guess 5 times and then it just takes them back to main and they can guess again, but I'm looking for something like this. If they guess 5 times and get all 5 wrong, take them back to main, but they can't enter bofh and if they do it just ignores it.

Does that make sense?

Thanks for the help.

jdm

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Here's one quick thought:

bool bad_guess = false;
bool let_him_in = false;
int counter = 0;

while( !bad_guess && !let_him_in )
{
     //get password from user
     if( password is bad )
     {
        counter++;
        if( counter == 5 )
            bad_guess = true;
     }
     else
         let_him_in = true;
}

Something like this (maybe a bit slicker) - let_him_in indicates a good password has been entered. bad_guess indicates user hit the 5 try limit, and once set you can use that elsewhere to determine allowable actions for user.

Thanks for the suggestion. I will give it a try and let you know, but it probably be a day or two before I get back to you. It all depends on my school work ;)

I get the concept behind this and all, but how would this be used in like that void bofh() function.

How would I tell main not to allow cin to accept the command bofh and not to call void bofh() when that command is enter.

Would I pass main something like this.

password has been wrong for 5 times pass that to main.
Main: if password is wrong 5 times the bool is true so don't allow cin to accept bofh.

The only problem I'm having is figuring out how to tell main not to allow that specific command, but allow other commands.

Thanks again

-jdm

Using my sample above, any point in main( ) where use might choose some option

if( !bad_guess )
{ 
    //allow to do all allowable actions
}
else
{
   //can only do limited set of actions
}

or preface any branching to an action with a test for permission.

Okay that makes more sense. I will give it a try and let you know if I get it to work.

Thanks again for the help.

-jdm

This article has been dead for over six months. Start a new discussion instead.