# Validation logic

#### King2

Joined Jul 17, 2022
80
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.

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;
}
Test logic output
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
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

Last edited:

#### WBahn

Joined Mar 31, 2012
27,394
And you question or point is...?

I don't follow your reasoning or goal here. Why does your program look five times? Why do you only check for card 2 and 5? You said that your program was supposed to open the door if any valid card was scanned, yet it doesn't do that.

#### King2

Joined Jul 17, 2022
80
Why does your program look five times?
good suggestion i don't need for loop

Why do you only check for card 2 and 5?
I just assumed I only have two cards available to test

You said that your program was supposed to open the door if any valid card was scanned, yet it doesn't do that.
Print statement is executed only after validation

#### King2

Joined Jul 17, 2022
80
It look's right to me because it verify all possible conditions if i didn't miss to check anything
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)
{
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 ) || ( input_card == 5 ))
{
printf("Valid, Door open, come in, door closed \n");
}

else
{
printf(" Invalid \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;
}
Test condition output
Code:
scan card
3
card number = 3
Invalid
Switch condition
0
Switch status = 0
scan card
2
card number = 2
Valid, Door open, come in, door closed
Switch condition
0
Switch status = 0
scan card
4
card number = 4
Invalid
Switch condition

#### King2

Joined Jul 17, 2022
80
I found a problem that when I am not scanning any card then I am getting invalid message which I do not want the program to print like this.

Invalid message should be printed only when someone trying to scan an invalid card.

I am lost in this logic,

#### Ya’akov

Joined Jan 27, 2019
6,561