Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   can anyone solve this problem for me (http://www.go4expert.com/forums/solve-t28483/)

azeeez 30May2012 14:06

can anyone solve this problem for me
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.


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.

pein87 30May2012 21:20

Re: can anyone solve this problem for me
Create an array of ten numbers that are unique.

Code: Cpp

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: Cpp

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.

azeeez 30May2012 21:52

Re: can anyone solve this problem for me
this is great i learned alot. i'll write it down and post it to check it with you.

xpi0t0s 31May2012 03:26

Re: can anyone solve this problem for me
I like using state machines for this kind of stuff.


switch (state)
case 0: // door is locked and we want a PIN
  state = PIN_valid() ? 1 : 2;
case 1: // door is locked and we have a valid PIN
case 2: // door is locked and we had 1 invalid PIN
  state = PIN_valid()
    ? 1  // we can open the door
        : 3; // next prompt
case 3: // door is locked and we had 2 invalid PINs
  state = PIN_valid()
    ? 1  // can open the door
        : 4; // next prompt
case 4: // door is locked and we had 3 invalid PINs
  // the description doesn't say what happens next; let's assume
  // the previous function returns when the alarm has been reset
  state = 0;

All times are GMT +5.5. The time now is 17:42.