I need your help guys with this

May 30, 2012 at 4:47am
I got this problem:

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.

....................

i done it with only 1 user (one password) heres the code:
#include <iostream>
using namespace std;

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
void main()
{
int pin=1234;

int pin2=0;
int cont=0;

while(cont<3)
{
if (pin2==0)
{
std::cout << "please enter your pin number\n" ;
std::cin >> pin2;
}
if ( pin==pin2)
{
std::cout << "Door opens\n" ;
cont=4;

}
else
{
cont++;
pin2=0;
}
if (cont==3)
{ std::cout << "Alarm\n" ;
}
}
}

....................

if any one can help me do it for 10 supporters that will be great i need it.
Last edited on May 30, 2012 at 5:00am
May 30, 2012 at 5:44am
if u are planning for only 10 passwords, have a const array like
 
const int pins[]={102,1123,1145,234};

store all the pins in it.
ask the user to enter the pin,Traverse through pin array, if the user entered value is found open door.
if u are planning for a practical application a more sophisticated approach will be required which will make use of custom encrypted container to store the pins.
Last edited on May 30, 2012 at 5:46am
May 30, 2012 at 6:02am
thanks man for you help but i'm a beginner,
I will apreciat it if you write the program for me.

thanx again man
May 30, 2012 at 6:12am
rough code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const int pins[]={1,2,3,4,5};
int pincorrect=0;
int userpin=0;
cout<<"Please enter the pin";
cin>>userpin;
//assuming the pins array to be of size 5
for(int i=0;i<5; i++)
{
  if(userpin == pins[i])
    {
       pincorrect=1;
        break;
     }
}
if(pincorrect == 1)
{
cout<<"open door";
}
else
cout<<"invalid pin, door remains closed";
May 30, 2012 at 6:38am
it's like any pin i put it will open,

thanks for the affort
May 30, 2012 at 11:43am
closed account (zb0S216C)
Maybe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int Pin(1234);
int EnteredPin(0);

for(int Attempts(1); Attempts <= 3; ++Attempts)
{
    std::cin >> EnteredPin;
    if(EnteredPin == Pin)
    {
        std::cout << "Correct!\n";
        break;
    }

    std::cout << "Wrong!\n";
}

With this code, you get 3 attempts at guessing the pin. With each failed attempt, "Wrong!" is printed to the screen which then increments the Attempts counter. Otherwise, "Correct!" is printed and the loop ends.

Wazzak
Last edited on May 30, 2012 at 11:45am
May 30, 2012 at 1:28pm
thanks alot man i learned alot from you,
and it works ok,
but there's in the problem he needs it for 10.
Assume that your security door system supports 10 users each with a unique PIN.

that's my problem i know i need array and loop i dont know how to deal with them
i know i'm asking alot but i'll apreciat it if you can write for me so i can learn how to do this kind of problems.
May 30, 2012 at 1:44pm
May 30, 2012 at 4:16pm
about the heads up yes i know i posted the same in another forum couse i didn't want to bother him he did enough and helped me and learned from him and learned from the other forums thanks all for your time and i see it's being a problem here so i apoligize.

i'm just trying to get the experiance on how to write they gave me useful hints i can work on them.
Topic archived. No new replies allowed.