I saw a system in which the door of the room opens on scanning a valid ID card. For those who do not have a valid ID card and scan their card, an invalid card message is displayed.If someone wants to leave the room, he presses the button and the door opens. A card is needed to enter the room and just press the switch to leave.
I have thought for practice that I will write a program for such a system.
We have 5 valid cards numbered 1, 2, 3, 4 and 5. Scanning any of these cards opens the door.
To check the program, I'll assume that I only have numbers 2 and 5 of the ID card.
Test logic output
I am not able to implement logic for invalid card in my code. I want to print the message Invalid ID card when invalid card is scanned else print " Scan id " in my program
I have thought for practice that I will write a program for such a system.
We have 5 valid cards numbered 1, 2, 3, 4 and 5. Scanning any of these cards opens the door.
To check the program, I'll assume that I only have numbers 2 and 5 of the ID card.
C:
#include<stdio.h>
int main()
{
int i = 0;
int input_card;
int input_switch;
int CARD_Numbers[5] = { 1, 2, 3, 4, 5};
while (1)
{
for ( i = 0; i < 5; i++)
{
printf("scan card \n");
scanf("%d", &input_card); // Enter any 1, 2, 3, 4 or 5 for valid or press other for invalid
printf("card number = %d \n", input_card);
if ( input_card == 2 )
{
printf("Valid, Door open, come in, door closed \n");
}
if ( input_card == 5 )
{
printf("Valid, Door open, come in, door closed \n");
}
printf("Switch condition \n");
scanf("%d", &input_switch); // 1 to open door or any number to close door
printf("Switch status = %d \n", input_switch);
if ( input_switch == 1 ) // Press 1 to open gate
{
printf("Door open, come out, door closed \n");
}
}
}
return 0;
}
Code:
scan card
2
card number = 2
Valid, Door open, come in, door closed
Switch condition
0
Switch status = 0
scan card
5
card number = 5
Valid, Door open, come in, door closed
Switch condition
1
Switch status = 1
Door open, come out, door closed
scan card
Last edited: