Thread: Write a password prompt that gives a user only a certain attemts

  1. #1
    Registered User amdriod's Avatar
    Join Date
    Mar 2015
    Location
    Pakistan
    Posts
    5

    Post Write a password prompt that gives a user only a certain attemts

    I'm trying to write this c++ programe:

    Write a password prompt that gives a user only a certain number of password entry attempts—
    so that the user cannot easily write a password cracker.
    Using FOR Loop. I know this can be easily done using while loop. But I want to know how to do it with for loop aswell. Here is what I have so far:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
        string pass;
        for (int t=0;t>=5;t++)
        {
        cout<<"Enter your password. You have "<<t<<" tries left"<<endl;
        cin>>pass;
        if (pass=="ahmad")
        {
            cout<<"Acess Granted!";
            break;
        }
        else
        {
            cout<<"Try again you have "<<t<<" attempts left"<<endl;
        }
        }
        
    }
    I don't see where the problem is.. The program just executes and does nothing. Please help.

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Compare your for loop version with your while loop version and you should discover the mistake. If you still cannot see it, then show us the version with the while loop that works. If you have not written such a while loop version, then write it, since you appear so confident of getting it right that perhaps you will be able to focus on the logic and not get distracted by the syntax, hence getting it right. After that you can compare with your for loop version.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User amdriod's Avatar
    Join Date
    Mar 2015
    Location
    Pakistan
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Compare your for loop version with your while loop version and you should discover the mistake. If you still cannot see it, then show us the version with the while loop that works. If you have not written such a while loop version, then write it, since you appear so confident of getting it right that perhaps you will be able to focus on the logic and not get distracted by the syntax, hence getting it right. After that you can compare with your for loop version.
    Here is my while loop version which works perfectly fine as expected:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
    	string pass;
    	int a=5;
    	
    	while (a>0)
    	{
    		cout<<"Enter your password. You have "<<a<<" tries left"<<endl;
    		cin>>pass;
    		if (pass=="ahmad")
    	{
    		cout<<"Acess Granted!";
    		break;
    	}
    	a--;
    		
    		
    	}
    	
    	if (a=0)
    	{
    		cout<<"Sorry 5 Wrong attempts";
    	}
    	
    	
    
    
    }
    and I've been able to make the for loop version work by modifying it a bit. But the problem remains the same. I want to figure out why didn't this code that I posted as my question work? If has the correct syntax and I'm missing logic somewhere but can't get the hand of it.

    Here is my modified version of for loop which made it work:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
    	string pass;
    	for (int t=5;t>0;)
    	{
    	cout<<"Enter your password. You have "<<t<<" tries left"<<endl;
    	cin>>pass;
    	if (pass=="ahmad")
    	{
    		cout<<"Acess Granted!";
    		break;
    	}
    	else
    	{
    		t--;
    		cout<<"Try again you have "<<t<<" attempts left"<<endl;
    	}
    	}
    	
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amdriod
    Here is my while loop version which works perfectly fine as expected:
    Enter the incorrect password 5 times and you will find that it does not print "Sorry 5 Wrong attempts". The reason is that you wrote if (a=0) instead of if (a == 0).

    Quote Originally Posted by amdriod
    and I've been able to make the for loop version work by modifying it a bit. But the problem remains the same. I want to figure out why didn't this code that I posted as my question work? If has the correct syntax and I'm missing logic somewhere but can't get the hand of it.
    Notice that in this working for loop version, your loop condition is t>0 and you wrote t--. In your previous attempt, your loop condition was t>=5 and you wrote t++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User amdriod's Avatar
    Join Date
    Mar 2015
    Location
    Pakistan
    Posts
    5
    Hahahahaha I feel so stupid. Thank you laserlight. the solution was pretty easy but I over-thought on it :P

    Thank you for the help!

  6. #6
    Registered User amdriod's Avatar
    Join Date
    Mar 2015
    Location
    Pakistan
    Posts
    5
    oh wait, in the orignal question my condition was t>=5 and was incrementing by t++ because it was initialized at 0 (int t=0)

    for(intt=0;t>=5;t++)

    so isn't this the correct format and it should work?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    0 >= 5 is false, so the loop does not even run for a single iteration.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    The middle expression in a for look, and the expression in a while loop both evaluate to true or false, they both will execute their blocks only if the condition evaluates to true:

    Code:
        for (int n = 0; n < 5; n++); // Runs 5 times with values of n being {0,4} going in a positive direction.
        for (int n = 4; n >= 0; n--); // Runs 5 times with values of n going from 4 to 0.
    With while loops a quick way to limit the iterations might be:
    Code:
        int numTries = 5;
        std::string password("");
    
        while (n--)
        {
            password = promptUserForPassword();
            if (validatePassword(password))
            {
                break;
            }
        }
    Or to really whittle it down:
    Code:
        int numTries = 5;
    
        while (n--)
        {
            if (validatePassword(promptUserForPassword()))
            {
                break;
            }
        }
    You could also selectively inject hints, based on the condition of an incorrect password:
    Code:
        int numTries = 5;
        bool failedAttempt = false;
        std::string hint("Hint: My girl!"); // A real world hint! :P
    
        while (n--)
        {
            if (failedAttempt)
            {
                std::cout << hint << "\n";
            }
            // alternate: // std::cout << (failedAttempt ? hint + "\n" : "");
    
            if (validatePassword(promptUserForPassword()))
            {
                break;
            }
            // We can only get here if the password is incorrect:
            failedAttempt = true;
        }
    Last edited by Alpo; 03-12-2015 at 12:09 AM.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  9. #9
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Late edit to post above:

    Where 'n' appears in the conditions of the loops I wrote, I meant to put the variable name "numTries" (which I renamed for clarity, and then forgot to update the rest of the code :S)
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  10. #10
    Registered User amdriod's Avatar
    Join Date
    Mar 2015
    Location
    Pakistan
    Posts
    5
    Oh, Thank you very much Alpo and laserlight. Now I understand where the problem was

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. prompt user for number of loops
    By wonderpoop in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 06:58 AM
  2. Changing this to user prompt help.
    By Infuriate in forum C Programming
    Replies: 5
    Last Post: 12-04-2005, 05:01 PM
  3. Replies: 5
    Last Post: 05-06-2004, 09:14 AM
  4. User prompt question
    By Joe_Gibbs in forum C++ Programming
    Replies: 6
    Last Post: 04-17-2004, 09:26 AM
  5. Password prompt in unix w/o \b
    By rafe in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 08:54 AM

Tags for this Thread