Validation logic

Thread Starter

King2

Joined Jul 17, 2022
163
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
29,979
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.
 

Thread Starter

King2

Joined Jul 17, 2022
163

Thread Starter

King2

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

Thread Starter

King2

Joined Jul 17, 2022
163
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
9,072
One problem is that in real life your scanner peripheral will tell your program (by interrupt) or your program can ask it (by polling) if there is a card to read. If not, your loop would return to the top.

So, you need a conditional that deals with that. The loop should never get to the reading of the card unless there is one to read.

It would probably be really helpful if you bought a cheap RFID reader and either a Raspberry Pi (if you want to stick with strict C) or any Arduino compatible board (like this one, for example) to use Arduino’s mostly C++ programming. and build a “working” system.

The wiring part of the project is a few jumpers (you will need a breadboard and “DuPont wires” (like this set, for example) if you don’t have them) and then you will have all the requirements of the real program and can see if it actually works as expected.

Your abstracted version lacks the experience needed to know what you need to include, hence the suggestion you really build it and learn.
 
Top