0% found this document useful (0 votes)
10 views

Documentnn

The document contains code for a C program that manages user accounts. It defines a user struct and functions to add users, view users, and authenticate an admin user. The program allows an admin to add or view users after logging in.

Uploaded by

rishit4908
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Documentnn

The document contains code for a C program that manages user accounts. It defines a user struct and functions to add users, view users, and authenticate an admin user. The program allows an admin to add or view users after logging in.

Uploaded by

rishit4908
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

#define MAX_USERS 100

Struct User {
Char username[50];
Char password[50];
Int isAdmin;
Char phoneNumber[20];
Char address[100];
Char name[50];
};

Struct User users[MAX_USERS];


Int numUsers = 0;

Void addUser(char username[], char password[], int isAdmin, char phoneNumber[], char
address[], char name[]) {
If (numUsers < MAX_USERS) {
Strcpy(users[numUsers].username, username);
Strcpy(users[numUsers].password, password);
Users[numUsers].isAdmin = isAdmin;
Strcpy(users[numUsers].phoneNumber, phoneNumber);
Strcpy(users[numUsers].address, address);
Strcpy(users[numUsers].name, name);
numUsers++;
printf(“User %s added successfully.\n”, username);
} else {
Printf(“Cannot add user. Maximum user limit reached.\n”);
}
}

Void viewUser(char username[]) {


For (int I = 0; I < numUsers; i++) {
If (strcmp(users[i].username, username) == 0) {
Printf(“Username: %s\n”, users[i].username);
Printf(“Password: %s\n”, users[i].password);
Printf(“Is Admin: %d\n”, users[i].isAdmin);
Printf(“Phone Number: %s\n”, users[i].phoneNumber);
Printf(“Address: %s\n”, users[i].address);
Printf(“Name: %s\n”, users[i].name);
Return; // Add return statement to exit the function
}
}
Printf(“User not found.\n”); // Add message to indicate user not found
}

Int main() {
Char inputUsername[50];
Char inputPassword[50];

Printf(“Enter admin username: “);


Scanf(“%49s”, inputUsername); // Use %49s to prevent buffer overflow
Printf(“Enter admin password: “);
Scanf(“%49s”, inputPassword); // Use %49s to prevent buffer overflow

If (strcmp(inputUsername, “RISHIT”) == 0 && strcmp(inputPassword, “RISHIT@911”) ==


0) {
Printf(“Welcome, admin!\n”);
Printf(“You have admin privileges.\n”);

// Allow admin to choose action


Int choice;
Printf(“1. Add user\n2. View user\nEnter your choice: “);
Scanf(“%d”, &choice);

Switch (choice) {
Case 1:
// Allow admin to add new user
Char newUsername[50];
Char newPassword[50];
Int newIsAdmin;
Char newPhoneNumber[20];
Char newAddress[100];
Char newName[50];

Printf(“Enter new username: “);


Scanf(“%49s”, newUsername); // Use %49s to prevent buffer overflow
Printf(“Enter new password: “);
Scanf(“%49s”, newPassword); // Use %49s to prevent buffer overflow
Printf(“Is new user an admin? (1 for yes, 0 for no): “);
Scanf(“%d”, &newIsAdmin);
Printf(“Enter new phone number: “);
Scanf(“%19s”, newPhoneNumber); // Use %19s to prevent buffer overflow
Printf(“Enter new address: “);
Scanf(“%99s”, newAddress); // Use %99s to prevent buffer overflow
Printf(“Enter new name: “);
Scanf(“%49s”, newName); // Use %49s to prevent buffer overflow

addUser(newUsername, newPassword, newIsAdmin, newPhoneNumber, newAddress,


newName);
break;

case 2:
// Example usage: view user
Printf(“Enter username to view: “);
Scanf(“%49s”, inputUsername); // Use %49s to prevent buffer overflow

viewUser(inputUsername);
break;

default:
printf(“Invalid choice.\n”);
break;
}
} else {
Printf(“Invalid admin credentials.\n”);
}
Return 0;
}

You might also like