Password Prompt Program

Hey guys. I'm encountering a problem I'm not sure how to solve. I made a slight change and I do not know where I made a mistake. Here is the problem I'm trying to solve.

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.

I'm trying to limit the attempts to five.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>

using namespace std;

int main()
{


   string input;
   int x;


   for(x=5;x<0;x--)
   {

       cout<<"Please enter the correct password"<<endl;

        cin>>input;

        if(input=="oddball")
        {
            cout<<"Access granted";

        }

        else
    {
        cout<<"Invalid password, you have"<<x<<"attempts left\n";
    }

}}
Last edited on
Line 14 is the problem. It never enters the loop because x is not less than zero to begin with.

It should be for(x=5; x>=0; x--)
closed account (48T7M4Gy)
x>0, not x<0 then iron out how to get out of the loop. 'while' instead of 'for' might be a good move :-)
Last edited on
Thanks guys :}
The standard idiom for this is:

for (int x = 0; x < 5; x++)

Then you have 4-x attempts left at the end of each loop.

Hope this helps.
Topic archived. No new replies allowed.