Password check with 3 attempts

This is a discussion on Password check with 3 attempts within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I was messing around for hours yesterday trying to get a nested for loop working for a password ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127

    Password check with 3 attempts

    Hi everyone,

    I was messing around for hours yesterday trying to get a nested for loop working for a password checker when it dawned on me that perhaps I could use a different type of loop instead.

    For hours, I made a bunch of loop programs so that I could better visualize what was happening (rather than just repeated numbers), like this one:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
        int numberOfLoops = 0;
        string newLine;
        
        cout << "How many repeats do you want? ";
        cin >> numberOfLoops;
        cout << "Want the loops separated by a new line? (y/n): ";
        cin >> newLine;
        
        for (int i = 0; i < numberOfLoops; i++)
        {
            cout << "I'm a loop item, let's celebrate!!";
            if ( newLine == "y" )
            {
                cout << '\n';
            }        
        }
        cout << "Loop repeated " << numberOfLoops << " times!";
    }
    I left it yesterday to come back fresh this morning and came up with this:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        
        string password;
        int attempts = 1;
        
        cout << "Please enter your password: ";
        getline(cin, password, '\n');
        
        while ( password != "123" && attempts < 3 )
        {
            cout << "Please try again: ";
            getline(cin, password, '\n');
            attempts++;
        }
        
        if ( attempts < 3 )
        {
            cout << "Access granted";
        }
        else
        {
            cout << "Sorry, only allowed 3 attempts";
        }
        
    }
    I guess my question this time is that because of my inexperience, I don't know yet which type of loop to choose. I just (maybe naively) presumed that for loops could handle anything and would also cut down on the amout of code.

    Is a while loop perfectly suitable for a password check?

    I'm sure there's a 'better' way of doing this but a bit stumped for ideas! Any input from you guys/girls is more than appreciated. You've all been a great help so far.

    Thanks.

    Sam.
    Last edited by samwillc; 06-09-2012 at 02:00 AM.

  2. #2
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127
    Actually, it doesn't work as expected on further testing.

    If a user gets the password wrong twice, then gets it right the third time, my program stills says "Sorry, only allowed 3 attempts". Back to the drawing board!

    Sam.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    No, no...you're on the right track. Just think; what other condition must be the case if attempts is 3 for the login to fail?

    As far as loop type to use, it doesn't really matter as long as you've got the logic correct.
    Last edited by rags_to_riches; 06-09-2012 at 03:27 AM.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    >> if ( attempts < 3 )
    So in the worst case scenario, isn't attempts going to be three? It's not failure if the password is right and it's the third attempt.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127
    Thanks.

    The conditions are as follows: Attempts needs to be 3 or less. Password needs to be correct.

    Code:
      #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        
        string password = "123";
        string passwordEntry;
        int attempts = 1;
        
        cout << "Please enter your password: ";
        getline(cin, passwordEntry, '\n');
        
        while ( passwordEntry != password && attempts <=2 )
        {
            cout << "Please try again: ";
            getline(cin, passwordEntry, '\n');
            attempts++;    
        }
        
        if ( passwordEntry == password && attempts <=3 )
        {
            cout << "Access granted";
        }
        else
        {
            cout << "Sorry, only allowed 3 attempts";
        }
        
    }
    The way I see it:

    1) The first attempt does not update the attempts variable
    2) So, it's actually less than or equal to 2 attempts within the loop as a user has already had one go
    3) If they get it right first go, the loop is skipped entirely
    4) If they get it right from within the loop, needs to exit loop so conditions must be false after this happens
    5) Once loop is exited, the if checks whether the password is correct or not AND whether it has been input correctly in 3 goes or less.

    Maybe I'm overcompicating this. In my code, I need to input the password twice which seems a bit odd. **edit** <---- changed this in above code so a string holds the password at the get go

    Sam.
    Last edited by samwillc; 06-09-2012 at 03:41 AM.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    >> 2) So, it's actually less than or equal to 2 attempts within the loop as a user has already had one go
    Or < 3

    >> I need to input the password twice which seems a bit odd.
    Yeah, but the user doesn't.

    >> Maybe I'm overcompicating this.
    It really doesn't seem like it to me. The other logically equivalent structures are not hard to discover, and they all have different "ugly" things about them, and they all work.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127
    Good points made by everyone, it's all very useful, thanks. It works as expected now, need to move onto functions. I imagine that will also kick up a bunch of problems! Still enjoying it though

    Sam.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error with function that attempts to fill an array
    By RRTT in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2011, 09:07 PM
  2. Reporting SSH break-in attempts
    By _Mike in forum Tech Board
    Replies: 7
    Last Post: 03-25-2010, 01:22 PM
  3. How to check somebody's password?
    By marinus in forum Linux Programming
    Replies: 8
    Last Post: 09-10-2005, 10:17 AM
  4. Simple login/password check
    By ceevee in forum C++ Programming
    Replies: 26
    Last Post: 01-16-2005, 11:05 PM
  5. Check if input password is correct
    By kagemand in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2001, 08:28 AM

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