can anyone solve this problem for me

azeeez's Avatar, Join Date: May 2012
Newbie Member
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.
pein87's Avatar
Ambitious contributor
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's Avatar, Join Date: May 2012
Newbie Member
this is great i learned alot. i'll write it down and post it to check it with you.
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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;
}