Password Program Question

This is a discussion on Password Program Question within the C++ Programming forums, part of the General Programming Boards category; So yea I'm new to programming. I've been trying to come up with some original programs and decided to make ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    11

    Password Program Question

    So yea I'm new to programming. I've been trying to come up with some original programs and decided to make a password program. My problem is that i can't figure out how to get the program to accept multiple passwords. I know there is a way to do it with strings but i can't figure it out.

    *just a side note it's probably pretty sloppy*
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        string password[4];
        password[0]="111111";
        password[1]="222222";
        password[2]="333333";
        password[3]="444444";
        string entry;
        string crctpass;
        int evaluate;
        cout<<"Please type in your password:";
        for(int x=0;x<5;x++)
        {
                        crctpass=password[t];
                        cin>>entry;
                        if(entry==crctpass)
                        {
                                          cout<<"You have entered the correct password!\n";
                                          evaluate=1;
                                          break;
                         }
                        else if(x<4)
                        {
                            cout<<"Incorrect, enter your password again:\n";
                         }
                        else
                        {
                            cout<<"Too many failed attempts.\n";
                            evaluate=0;
                         }
        }
        if(evaluate==1)
        {
                      cout<<"Program booting...\n";
         }
         else
         {
                      cout<<"Shutting down...\n";
          }
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    That's what i have so far. I can't figure out how to loop it so variable 't' increments changing the password that is checked without messing up the loop that allows the user to try and input different passwords.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Add a loop outside all the code you want to repeat. But an opening brace in front of the first line you want to be part of your new loop, and a closing brace after the last line, then just add the loop control above the opening brace.

    So the first step is identifying which code you want to repeat. Can you do that?

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    The code that I'm repeating is the code that checks the password

    Code:
    if(entry==crctpass)
                        {
                                          cout<<"You have entered the correct password!\n";
                                          evaluate=1;
                                          break;
                         }
                        else if(x<4)
                        {
                            cout<<"Incorrect, enter your password again:\n";
                         }
                        else
                        {
                            cout<<"Too many failed attempts.\n";
                            evaluate=0;
                         }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Ok, but you don't want to repeat all of that, do you? You don't want to output "Incorrect, ..." until you have checked all four password, right? (That's an honest question, I'm not entirely sure what you're trying to do.)

    You really just need to have a loop around the code that checks the password itself. Hint: The else if in your current code doesn't have to be an else if, it can be an if, because the actual if part breaks out of the loop anyway. That might help you get an idea of where to break the code up and what to put into the new loop.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Yea, i don't want it to output "incorrect" for each check of the passwords. I used the for loop to give the user 5 attempts to get the password right before shutting down to prevent users from just trying a million different passwords. I want to nest a loop inside the for loop so that it checks each password to see if it matches any of the 4 passwords, while still limiting the user to trying only 5 passwords. I have really confused myself.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Write out in english the first few steps you want to do inside the loop you already have. You want to loop 5 times, giving the user 5 attempts to guess one of the passwords inside the password array. So what steps do you need to do that?

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Inside the for loop, i need to create a loop to check each password in the array. Once the password is correct, I'll break the loops and make the console write "program booting".

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Okay, I got the program to accept multiple inputs with

    Code:
        for(x=1;x<=5;x++)
        {
                         if(entry==password[t])
                         {
                                               cout<<"You have entered the correct password!\n";
                                               evaluate=1;
                                               break;
                         }
                         else if(x<=4)
                         {
                              cout<<"Incorrect, enter your password again:";
                         }
                         else
                         {
                             cout<<"Too many failed attempts";
                             evaluate=0;
                             break;
                             }
                         t++;
        }
    But i am having the problem you described earlier, if i enter "444444", it displays incorrect 4 times and then says correct all in one line. How would i fix that? I understand that i need to take that out of the loop, but how can i do that without getting rid of the "Incorrect" if they put in a wrong password?

    Also if i input a wrong password, the loop runs all 5 times and shuts down automatically.
    Last edited by SirTalkAlot415; 11-05-2007 at 06:32 PM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Change the else if to an if and make the inner loop smaller so it only covers the code that should repeat.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Okay, the program now accepts any of the four passwords. My revised code is

    Code:
        for(x=1;x<=5;x++)
        {
                         cin>>entry;
                         while(entry!=password[t])
                         {
                                                  t++;
                         }
                         if(entry==password[t])
                         {
                                               cout<<"You have entered the correct password!\n";
                                               evaluate=1;
                                               break;
                         }
                         if(x<=4)
                         {
                              cout<<"Incorrect, enter your password again:";
                         }
                         else
                         {
                             cout<<"Too many failed attempts.\n";
                             evaluate=0;
                             break;
                         }
        }
    But i have a problem now. If i enter an incorrect problem, it says that the .exe file had an error and had to close. Any idea what that's about?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Any idea what that's about?
    Yeah, look at your new loop. It does a good job of looping through the four password possibilities, but the only way the loop will exit is if entry!=password[t] is false (which happens when they enter one of the passwords). You need to make the loop exit when you run out of passwords to check as well.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Thank you for all of your help. I have the programming running without any found bugs. My final output is

    Code:
    for(x=1;x<=5;x++)
        {
                         cin>>entry;
                         for(t=0;entry!=password[t];t++)
                         {
                                                  if(t==3){break;};
                         }
                         if(entry==password[t])
                         {
                                               cout<<"You have entered the correct password!\n";
                                               evaluate=1;
                                               break;
                         }
                         if(x<=4)
                         {
                              cout<<"Incorrect, enter your password again:";
                         }
                         else
                         {
                             cout<<"Too many failed attempts.\n";
                             evaluate=0;
                             break;
                         }
        }
    Thanks again!

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > for(x=1;x<=5;x++)
    Given array[N] (in your case, N=4), this loop fails to hit the start of the array and overruns the end of the array.
    for ( x = 0 ; x < 4 ; x++ )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I thought that too at first but the OP is not using x as an index for that array, so the loop is fine. It gives the user 5 tries to guess the password and is unrelated to the 4 element array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. I need help with a password program
    By italiano40 in forum C++ Programming
    Replies: 15
    Last Post: 08-23-2006, 10:27 PM
  3. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 09:04 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Question about K&R program
    By Aerie in forum C Programming
    Replies: 15
    Last Post: 04-24-2005, 07:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21