#include <stdio.h> #include <string.h> #define MAX_ATTEMPTS 3 #define MAX_USERS 5 #define MAX_USERNAME_LENGTH 20 #define MAX_PASSWORD_LENGTH 20 typedef struct { char username[MAX_USERNAME_LENGTH]; char password[MAX_PASSWORD_LENGTH]; } User; int main() { User users[MAX_USERS] = { {"user1", "pass1"}, {"user2", "pass2"}, {"user3", "pass3"}, {"user4", "pass4"}, {"user5", "pass5"} }; int attempts = 0; int authenticated = 0; while (attempts < MAX_ATTEMPTS && !authenticated) { char inputUsername[MAX_USERNAME_LENGTH]; char inputPassword[MAX_PASSWORD_LENGTH]; printf("Enter username: "); scanf("%s", inputUsername); printf("Enter password: "); scanf("%s", inputPassword); for (int i = 0; i < MAX_USERS; i++) { if (strcmp(inputUsername, users[i].username) == 0 && strcmp(inputPassword, users[i].password) == 0) { authenticated = 1; break; } } if (!authenticated) { attempts++; printf("Authentication failed, try again.\n"); } } if (authenticated) { printf("Authentication successful.\n"); } else { printf("Limit exceeded. Exiting.\n"); } return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter