Okay, here is an interactive C program using structures to manage a Trainer database for a small training company. This program includes the requested menu options: Create, Read, Modify, and Delete records.
#include <stdio.h>
#include <stdlib.h> // For exit(), system() - optional for clearing screen
#include <string.h> // For string manipulation functions like strcpy, strcmp
// --- Constants ---
#define MAX_TRAINERS 100 // Maximum number of trainers the database can hold
#define NAME_LEN 50
#define DEPT_LEN 50
#define POS_LEN 50
// --- Structure Definitions ---
// Structure for Date of Joining
typedef struct {
int day;
int month;
int year;
} Date;
// Structure for Trainer Record
typedef struct {
int trainerId; // Unique ID for the trainer
char trainerName[NAME_LEN];
char departmentName[DEPT_LEN];
float salary;
char position[POS_LEN];
Date dateOfJoining;
int isActive; // Flag to mark if the record is active (1) or deleted (0)
} Trainer;
// --- Global Variables ---
Trainer trainers[MAX_TRAINERS]; // Array to hold all trainer records
int trainerCount = 0; // Current number of active trainers in the database
// --- Function Prototypes ---
void displayMenu();
void createNewRecord();
void listRecords();
void modifyRecord();
void deleteRecord();
int findTrainerById(int id); // Helper function to find a trainer by ID
void clearInputBuffer(); // Helper function to clear residual input
void printTrainerDetails(int index); // Helper function to print details of one trainer
// --- Main Function ---
int main() {
int choice;
// Initialize all records as inactive initially (optional, good practice)
for (int i = 0; i < MAX_TRAINERS; ++i) {
trainers[i].isActive = 0;
trainers[i].trainerId = -1; // Indicate unused slot
}
do {
displayMenu();
printf("Enter your choice: ");
// Robust input for choice
if (scanf("%d", &choice) != 1) {
printf("Invalid input. Please enter a number.\n");
clearInputBuffer(); // Clear the invalid input
choice = -1; // Set choice to an invalid value to loop again
printf("\nPress Enter to continue...");
getchar(); // Consume the newline after clearing buffer
getchar(); // Wait for user to press Enter
continue; // Go back to the menu display
}
clearInputBuffer(); // Clear the newline character left by scanf
switch (choice) {
case 1:
createNewRecord();
break;
case 2:
listRecords();
break;
case 3:
modifyRecord();
break;
case 4:
deleteRecord();
break;
case 5:
printf("Exiting the application. Goodbye!\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
// Pause before showing the menu again (unless exiting)
if (choice != 5) {
printf("\nPress Enter to continue...");
getchar(); // Wait for user to press Enter
}
} while (choice != 5);
return 0;
}
// --- Function Definitions ---
// Clears the input buffer (e.g., after scanf)
void clearInputBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
// Displays the main menu
void displayMenu() {
// Optional: Clear screen (platform dependent)
// system("cls"); // For Windows
// system("clear"); // For Linux/macOS
printf("\n--- Trainer Database Management ---\n");
printf("1. Create New Record\n");
printf("2. List All Records\n");
printf("3. Modify Record\n");
printf("4. Delete Record\n");
printf("5. Exit\n");
printf("-----------------------------------\n");
}
// Finds the index of a trainer by ID. Returns index or -1 if not found or inactive.
int findTrainerById(int id) {
for (int i = 0; i < trainerCount; ++i) {
if (trainers[i].isActive && trainers[i].trainerId == id) {
return i; // Found the active trainer
}
}
return -1; // Not found or not active
}
// Prints details of a single trainer given their index
void printTrainerDetails(int index) {
printf("----------------------------------------\n");
printf(" Trainer ID : %d\n", trainers[index].trainerId);
printf(" Name : %s\n", trainers[index].trainerName);
printf(" Department : %s\n", trainers[index].departmentName);
printf(" Salary : %.2f\n", trainers[index].salary);
printf(" Position : %s\n", trainers[index].position);
printf(" Date of Joining : %02d-%02d-%d\n", trainers[index].dateOfJoining.day,
trainers[index].dateOfJoining.month, trainers[index].dateOfJoining.year);
printf("----------------------------------------\n");
}
// Creates a new trainer record
void createNewRecord() {
if (trainerCount >= MAX_TRAINERS) {
printf("Database is full. Cannot add more trainers.\n");
return;
}
int newId;
int existingIndex;
printf("\n--- Create New Trainer Record ---\n");
// --- Trainer ID Input and Validation ---
while (1) {
printf("Enter Trainer ID: ");
if (scanf("%d", &newId) != 1) {
printf("Invalid input. Please enter a numeric ID.\n");
clearInputBuffer();
continue;
}
clearInputBuffer();
// Check if ID already exists (among active trainers)
existingIndex = -1;
for (int i = 0; i < trainerCount; ++i) {
if (trainers[i].isActive && trainers[i].trainerId == newId) {
existingIndex = i;
break;
}
}
if (existingIndex != -1) {
printf("Error: Trainer ID %d already exists. Please use a unique ID.\n", newId);
} else if (newId <= 0) {
printf("Error: Trainer ID must be a positive number.\n");
}
else {
break; // Valid, unique, positive ID entered
}
}
trainers[trainerCount].trainerId = newId;
// --- Other Details Input ---
printf("Enter Trainer Name: ");
fgets(trainers[trainerCount].trainerName, NAME_LEN, stdin);
trainers[trainerCount].trainerName[strcspn(trainers[trainerCount].trainerName, "\n")] = 0; // Remove trailing newline
printf("Enter Department Name: ");
fgets(trainers[trainerCount].departmentName, DEPT_LEN, stdin);
trainers[trainerCount].departmentName[strcspn(trainers[trainerCount].departmentName, "\n")] = 0;
printf("Enter Salary: ");
while (scanf("%f", &trainers[trainerCount].salary) != 1 || trainers[trainerCount].salary < 0) {
printf("Invalid input. Please enter a non-negative numeric salary: ");
clearInputBuffer();
}
clearInputBuffer();
printf("Enter Position: ");
fgets(trainers[trainerCount].position, POS</string.h></stdlib.h></stdio.h>