# can anyone solve this problem for me

Discussion in 'C' started by azeeez, May 30, 2012.

1. ### azeeezNew Member

Joined:
May 30, 2012
Messages:
2
0
Trophy Points:
0
I need help with this problem can you solve it for me ( write the program)

-A password-protected security door can only be opened when a user inserts a valid 4-digit personal identification number (PIN).
-The program starts by asking the user to enter a 4-digit PIN.If it is valid, the locked door is opened and the program returns for another user
input. If, on the other hand, the PIN is invalid the program checks whether three attempts were made. If so, the alarm is triggered; otherwise the user is given another try to input the correct PIN.
-Assume that your security door system supports 10 users each with a unique PIN.

and i'll BE THANKFULL

it's not homework,

i have a final exam and this question was in one of the final exam sample
i just want to know how to solve it.

hope you can help.

2. ### pein87New Member

Joined:
Aug 6, 2010
Messages:
173
47
Trophy Points:
0
Occupation:
Web Dev
Location:
Limbo
Create an array of ten numbers that are unique.

Code:
```int pins[10] = { 1982,3455,4356,2467,2778,4578,5473,2468,8535,3732 };

int pinLen = 10;

int loginFlag = 0; // set to 0 and add one one each failed attempt and set to zero on new login```
you need to get data from the console and make sure that its length is 4 characters long.

Code:
```char input = cin.get();

if(strlen(input) < 4)
{

// code to do when its on 4

}
else if(strlen(input) == 4)
{

// handle rest of processing here

}```
you'd probably need to set a while loop that takes input as long as the loginFlag is not equal to 3. When it reaches three then you need to trigger an output message and block input for a fixed time. When that time is finished you then resume the input loop. I gave you some hints but thats as far as I'm willing to go with helping you on an exam.

3. ### azeeezNew Member

Joined:
May 30, 2012
Messages:
2
0
Trophy Points:
0
this is great i learned alot. i'll write it down and post it to check it with you.

4. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
I like using state machines for this kind of stuff.

Code:
```switch (state)
{
case 0: // door is locked and we want a PIN
get_PIN();
state = PIN_valid() ? 1 : 2;
break;

case 1: // door is locked and we have a valid PIN
open_door();
wait_door_close();
state=0;
break;

case 2: // door is locked and we had 1 invalid PIN
get_PIN();
state = PIN_valid()
? 1  // we can open the door
: 3; // next prompt
break;

case 3: // door is locked and we had 2 invalid PINs
get_PIN();
state = PIN_valid()
? 1  // can open the door
: 4; // next prompt
break;

case 4: // door is locked and we had 3 invalid PINs
sound_alarm();
// the description doesn't say what happens next; let's assume
// the previous function returns when the alarm has been reset
state = 0;
break;
}
```