Thread: Password check with 3 attempts (split)

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    2

    Password check with 3 attempts (split)

    What if I want to let the user know the number of trials left...? Like "2 attempts left", "1 attempt left"?...

  2. #2
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Quote Originally Posted by Shedrack Ayaro View Post
    What if I want to let the user know the number of trials left...? Like "2 attempts left", "1 attempt left"?...
    This is a very old thread! I haven't played with C++ for quite awhile but I imagine instead of going from 1 attempts to 3, maybe reverse that and start at 3 and use that counter as a text prompt. Something like this, from the top of my head:

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int main()
    {
        
        string password = "123";
        string passwordEntry;
        int attempts = 2;
        
        cout << "Please enter your password (you have 3 attempts): ";
        getline(cin, passwordEntry, '\n');
        
        while (passwordEntry != password && attempts > 0)
        {
            cout << "Please try again: ";
            getline(cin, passwordEntry, '\n');
            attempts--;
            cout << "You have " << attempts << " attempts left."
        }
        
        if (passwordEntry == password && attempts > 0)
        {
            cout << "Access granted";
        }
        else
        {
            cout << "Sorry, only allowed 3 attempts";
        }
        
    }
    As I said, it's been awhile, not sure if this is the correct way to concatenate a string in C++ (line 23, maybe it should be a '+'), or whether this actually works. I'm sure this will give you an idea though to complete it yourself.

    Good luck.

    Sam.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Password check
    By BIGDENIRO in forum C Programming
    Replies: 9
    Last Post: 01-07-2014, 08:37 PM
  2. Password check
    By jgkamin in forum C Programming
    Replies: 16
    Last Post: 02-19-2013, 07:55 PM
  3. Password check with 3 attempts
    By samwillc in forum C++ Programming
    Replies: 6
    Last Post: 06-09-2012, 03:54 AM
  4. How to check somebody's password?
    By marinus in forum Linux Programming
    Replies: 8
    Last Post: 09-10-2005, 10:17 AM
  5. Check if input password is correct
    By kagemand in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2001, 09:28 AM