mardi 3 août 2021

c program to check the user entered pin with the already set 4 digit pin

Enter a 4 digit Pin and check whether it is correct or not.

The program masks the 4 digit pin with * but the problem i am having is i want to ignore all the other characters except numbers and also i just want the user to enter exactly 4 digits.

I hope you guys understand my code

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define TAB 9
#define BKSP 8
#define SPACE 32

void main(){

    int pin = 1234 ;
    char apin[4] , ch;
    int attempt ;
    int i;
    int pw = 0; 

    for(attempt=1; attempt<=3; attempt++) {

    printf("Enter 4 digit pin code:\n");

    for(i=0;i<4;i++)
    {
        ch = getch();
       
        if(ch == BKSP)
        {
            if(i>0){
                i--;
                printf("\b \b");
            }
        }
        else if(ch==TAB || ch == SPACE ){
            continue;
        }
        else{
        apin[i] = ch;
        ch = '*' ;
        printf("%c",ch);
        }
    }
    apin[i] = ' ';
    printf("\n");
    pw = atoi(apin) ;
    //printf("%d\n",pw);//

    if(pw != 1234){
        printf("Invalid Pin\n");
        printf("You have %d attempts remaining\n",3-attempt);

    }
    else{
        printf("Your have entered the correct pin code\n");
        break;
    }
    }
}```

Aucun commentaire:

Enregistrer un commentaire