BABCOCK UNIVERSITY
COS 101 ASSIGNMENT
// Program to allow user to key in a passcode with 3 attempts
// Written by: [ADESIDA ADEWEMIMO OLUWADARASIMI]
// Matriculation Number: [24/1530]
#include <stdio.h>
#include <string.h>
int main() {
const char correct_passcode[] = "1234"; // Set the correct passcode here
char user_input[20]; // To store user input
int max_attempts = 3; // Maximum number of attempts
int attempts = 0; // Count of attempts made
while (attempts < max_attempts) {
printf("Enter your passcode: ");
scanf("%6s", user_input); // Get user input (max 6 characters to prevent
overflow)
if (strcmp(user_input, correct_passcode) == 0) {
printf("Access Granted!\n");
return 0; // Exit the program successfully
} else {
attempts++;
printf("Incorrect passcode. You have %d attempt(s) left.\n",
max_attempts - attempts);
}
}
// If the loop ends, it means all attempts failed
printf("Access Denied. Program terminated after 3 failed attempts.\n");
return 1; // Exit with error code
}