I'm a beginner in learning C programming language. I've been having some problems with my problem, i.e., pin masking with 3 attempts for my ATM banking system. Furthermore, I've used goto statement, however, we are restricted to use this statement. So how can I remove the goto statement and use loop instead? Badly need help. Thank you! Code is down below.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define ENTER 13
#define TAB 9
#define BCKSPC 8
int pinValidation()
{
char pwd[20], pin[]="5263";
int attemptCount=1;
char ch;
start:
int i=0;
//attempts count
do{
printf("Enter your pin: ");
while(1)
{
ch=getch();
if(ch == ENTER || ch == TAB)
{
pwd[i]='\0';
break;
}
else if(ch == BCKSPC)
{
if(i>0)
{
i--;
printf("\b \b"); //for the cursor
}
}
else if(pwd[i]=ch)
{
pwd[i]=ch;
i ;
printf("*");
}
}
}while(ch != ENTER);
if(strcmp(pin,pwd)==0 && attemptCount <= 4)
{
Beep(1000,500);
Beep(1000,600);
printf("\nAccess granted");
}
else
{
Beep(750, 800);
printf("\n\nUnsuccessful login %d of 3 attempt\n\n", attemptCount);
attemptCount ;
if (attemptCount < 4) goto start;
if (attemptCount == 4) {
goto reached;
}
getch();
reached:
Beep(800,800);
Beep(800,800);
Beep(800,900);
printf("Attempt reached");
}
getch();
}
int main()
{
pinValidation();
return 0;
}
/*
start
initialize variables
input pin
if pin is != inputed pin && attempt < 3
print unsuccessful login %d attempt left
else if pin != inputed pin && attempt = 3
print "attempt reached"
else if pin == inputted pin && attempt <= 3
print "Access granted"
*/
CodePudding user response:
This looks like a bit of course work?! ;-)
Nonetheless, hope this helps you understand the approach...
#include <stdio.h>
#include <stdbool.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define ENTER 13
#define TAB 9
#define BCKSPC 8
#define MAXATTEMPTS 4
#define MAXPINLENGTH 4
void getPin(char* pinCapture, size_t size)
{
char ch;
int pinLength = 0;
pinCapture[pinLength] = '\0';
printf("Enter PIN? ");
do
{
ch = getch();
switch (ch)
{
// Characters to ignore...
case ENTER:
case TAB:
break;
// Allow user to delete previous digits
case BCKSPC:
if (pinLength > 0)
{
pinLength--;
printf("\b \b");
}
break;
// Add character to PIN...
default:
pinCapture[pinLength] = ch;
pinLength ;
printf("*");
break;
}
} while (ch != ENTER && pinLength < size);
pinCapture[pinLength] = '\0';
printf("\n");
}
bool pinValidation()
{
char validPin[] = "1234";
int attemptCount = 0;
char pin[5];
while (attemptCount < MAXATTEMPTS && strcmp(pin, validPin) != 0)
{
attemptCount ;
getPin(pin, MAXPINLENGTH 1);
if (strcmp(pin, validPin) != 0)
{
printf("Invalid attempt. You have %d more trys.\n", MAXATTEMPTS - attemptCount);
}
}
if (strcmp(pin, validPin) == 0)
{
return true;
}
else
{
return false;
}
}
int main()
{
if (pinValidation())
{
printf("Access granted.\n");
}
else
{
printf("Access denied.\n");
}
return 0;
}
/*
start
initialize variables
input pin
if pin is != inputed pin && attempt < 3
print unsuccessful login %d attempt left
else if pin != inputed pin && attempt = 3
print "attempt reached"
else if pin == inputted pin && attempt <= 3
print "Access granted"
*/
