I need your help guys with this

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
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
thanks man for you help but i'm a beginner,
I will apreciat it if you write the program for me.

thanx again man
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";
it's like any pin i put it will open,

thanks for the affort
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
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.
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.