Question 1: Using Structures write an interactive program in C language to create an application
program for a small training company to maintain the Trainer’s database. This
application should be having menu options like (20 Marks)
• Creating a New Record
• Reading/Listing of Records
• Modify the record
• Delete the record
Each Trainer record should have Trainer Name, Trainer ID, Department Name, Salary, Position, Date of
Joining, etc.). The application should be designed user-friendly.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a comprehensive C program that meets your requirements for a Trainer Database Management System:
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_TRAINERS 100
#define FILENAME "trainers.dat"
typedef struct {
int day;
int month;
int year;
} Date;
typedef struct {
char name[50];
int id;
char department[50];
float salary;
char position[50];
Date joining_date;
} Trainer;
Trainer trainers[MAX_TRAINERS];
int num_trainers = 0;
// Function prototypes
void display_menu();
void create_record();
void list_records();
void modify_record();
void delete_record();
void save_to_file();
void load_from_file();
int find_trainer(int id);
int validate_date(Date d);
void clear_input_buffer();
int main() {
load_from_file();
int choice;
do {
display_menu();
printf("Enter your choice: ");
scanf("%d", &choice);
clear_input_buffer();
switch(choice) {
case 1: create_record(); break;
case 2: list_records(); break;
case 3: modify_record(); break;
case 4: delete_record(); break;
case 5: printf("Exiting...\n"); break;
default: printf("Invalid choice! Try again.\n");
}
} while(choice != 5);
save_to_file();
return 0;
}
void display_menu() {
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");
}
void create_record() {
if(num_trainers >= MAX_TRAINERS) {
printf("Database full! Cannot add more trainers.\n");
return;
}
Trainer new_trainer;
printf("\n--- New Trainer Entry ---\n");
// Input validation loop for ID
while(1) {
printf("Enter Trainer ID: ");
scanf("%d", &new_trainer.id);
clear_input_buffer();
if(find_trainer(new_trainer.id) == -1) break;
printf("ID already exists! Try another ID.\n");
}
printf("Enter Name: ");
fgets(new_trainer.name, 50, stdin);
new_trainer.name[strcspn(new_trainer.name, "\n")] = '\0';
printf("Enter Department: ");
fgets(new_trainer.department, 50, stdin);
new_trainer.department[strcspn(new_trainer.department, "\n")] = '\0';
printf("Enter Position: ");
fgets(new_trainer.position, 50, stdin);
new_trainer.position[strcspn(new_trainer.position, "\n")] = '\0';
// Salary validation
do {
printf("Enter Salary: ");
scanf("%f", &new_trainer.salary);
clear_input_buffer();
} while(new_trainer.salary <= 0);
// Date validation
Date date;
do {
printf("Enter Joining Date (DD MM YYYY): ");
scanf("%d %d %d", &date.day, &date.month, &date.year);
clear_input_buffer();
} while(!validate_date(date));
new_trainer.joining_date = date;
trainers[num_trainers++] = new_trainer;
printf("Record added successfully!\n");
}
void list_records() {
if(num_trainers == 0) {
printf("No records found!\n");
return;
}
printf("\n=== Trainer List ===\n");
printf("%-5s %-20s %-15s %-15s %-10s %-15s\n",
"ID", "Name", "Department", "Position", "Salary", "Joining Date");
for(int i = 0; i < num_trainers; i++) {
Trainer t = trainers[i];
printf("%-5d %-20s %-15s %-15s $%-9.2f %02d/%02d/%-4d\n",
t.id, t.name, t.department, t.position, t.salary,
t.joining_date.day, t.joining_date.month, t.joining_date.year);
}
}
void modify_record() {
int id, index;
printf("Enter Trainer ID to modify: ");
scanf("%d", &id);
clear_input_buffer();
index = find_trainer(id);
if(index == -1) {
printf("Trainer not found!\n");
return;
}
Trainer *t = &trainers[index];
printf("\nCurrent Details:\n");
printf("1. Name: %s\n", t->name);
printf("2. Department: %s\n", t->department);
printf("3. Position: %s\n", t->position);
printf("4. Salary: %.2f\n", t->salary);
printf("5. Joining Date: %02d/%02d/%d\n",
t->joining_date.day, t->joining_date.month, t->joining_date.year);
int choice;
printf("\nEnter field number to modify (1-5): ");
scanf("%d", &choice);
clear_input_buffer();
switch(choice) {
case 1:
printf("Enter new name: ");
fgets(t->name, 50, stdin);
t->name[strcspn(t->name, "\n")] = '\0';
break;
case 2:
printf("Enter new department: ");
fgets(t->department, 50, stdin);
t->department[strcspn(t->department, "\n")] = '\0';
break;
case 3:
printf("Enter new position: ");
fgets(t->position, 50, stdin);
t->position[strcspn(t->position, "\n")] = '\0';
break;
case 4:
do {
printf("Enter new salary: ");
scanf("%f", &t->salary);
clear_input_buffer();
} while(t->salary <= 0);
break;
case 5:
Date date;
do {
printf("Enter new joining date (DD MM YYYY): ");
scanf("%d %d %d", &date.day, &date.month, &date.year);
clear_input_buffer();
} while(!validate_date(date));
t->joining_date = date;
break;
default:
printf("Invalid choice!\n");
return;
}
printf("Record updated successfully!\n");
}
void delete_record() {
int id, index;
printf("Enter Trainer ID to delete: ");
scanf("%d", &id);
clear_input_buffer();
index = find_trainer(id);
if(index == -1) {
printf("Trainer not found!\n");
return;
}
for(int i = index; i < num_trainers - 1; i++) {
trainers[i] = trainers[i + 1];
}
num_trainers--;
printf("Record deleted successfully!\n");
}
// Helper functions
int find_trainer(int id) {
for(int i = 0; i < num_trainers; i++) {
if(trainers[i].id == id) return i;
}
return -1;
}
int validate_date(Date d) {
if(d.year < 1900 || d.year > 2025) return 0;
if(d.month < 1 || d.month > 12) return 0;
if(d.day < 1 || d.day > 31) return 0;
// Additional validation for different months
if((d.month == 4 || d.month == 6 || d.month == 9 || d.month == 11) && d.day > 30)
return 0;
if(d.month == 2) {
int leap = (d.year % 4 == 0 && d.year % 100 != 0) || (d.year % 400 == 0);
if(d.day > (28 + leap)) return 0;
}
return 1;
}
void save_to_file() {
FILE *file = fopen(FILENAME, "wb");
if(file == NULL) {
printf("Error saving data!\n");
return;
}
fwrite(#_trainers, sizeof(int), 1, file);
fwrite(trainers, sizeof(Trainer), num_trainers, file);
fclose(file);
}
void load_from_file() {
FILE *file = fopen(FILENAME, "rb");
if(file == NULL) return;
fread(#_trainers, sizeof(int), 1, file);
fread(trainers, sizeof(Trainer), num_trainers, file);
fclose(file);
}
void clear_input_buffer() {
int c;
while((c = getchar()) != '\n' && c != EOF);
}
Key Features:
User-Friendly Menu System: Simple numeric input system with clear prompts
Data Validation:
Unique Trainer ID check
Valid date checking (including leap years)
Positive salary validation
File I/O: Automatic saving/loading of data to "trainers.dat"
Error Handling: Clear error messages for invalid inputs
Clean Formatting: Tabular display of records
Modular Design: Separate functions for each operation
Usage Instructions:
Compile using any C compiler: gcc -o trainer_db trainer_db.c
Run the executable: ./trainer_db
Follow the menu prompts to manage records
The program will automatically save all changes when exiting and load previous data on startup. All data fields are validated to ensure data integrity.</ctype.h></string.h></stdlib.h></stdio.h>
User
will this also work? Solution 1 :
➤ Features to Implement:
•
Create a new record
•
Read/list records
•
Modify the record
•
Delete the record
Program :
#include <stdio.h>
#include <string.h>
#define MAX 100
struct Trainer {
int id;
char name[50];
char department[30];
float salary;
char position[30];
char dateOfJoining[20];
};
struct Trainer trainers[MAX];
int count = 0;
void addTrainer() {
printf("\nEnter Trainer ID: ");
scanf("%d", &trainers[count].id);
printf("Enter Name: ");
scanf(" %[^\n]", trainers[count].name);
printf("Enter Department Name: ");
scanf(" %[^\n]", trainers[count].department);
printf("Enter Salary: ");
scanf("%f", &trainers[count].salary);
printf("Enter Position: ");
scanf(" %[^\n]", trainers[count].position);
printf("Enter Date of Joining (dd/mm/yyyy): ");
scanf(" %[^\n]", trainers[count].dateOfJoining);
count++;
printf("Trainer record added successfully.\n");
}
void listTrainers() {
printf("\n--- Trainer Records ---\n");
for (int i = 0; i < count; i++) {
printf("ID: %d, Name: %s, Department: %s, Salary: %.2f, Position: %s, Date of Joining: %s\n",
trainers[i].id, trainers[i].name, trainers[i].department, trainers[i].salary, trainers[i].position, trainers[i].dateOfJoining);
}
}
void modifyTrainer() {
int id, found = 0;
printf("\nEnter Trainer ID to modify: ");
scanf("%d", &id);
for (int i = 0; i < count; i++) {
if (trainers[i].id == id) {
printf("Enter new Name: ");
scanf(" %[^\n]", trainers[i].name);
printf("Enter new Department: ");
scanf(" %[^\n]", trainers[i].department);
printf("Enter new Salary: ");
scanf("%f", &trainers[i].salary);
printf("Enter new Position: ");
scanf(" %[^\n]", trainers[i].position);
printf("Enter new Date of Joining: ");
scanf(" %[^\n]", trainers[i].dateOfJoining);
found = 1;
printf("Record updated.\n");
break;
}
}
if (!found) printf("Trainer not found.\n");
}
void deleteTrainer() {
int id, found = 0;
printf("\nEnter Trainer ID to delete: ");
scanf("%d", &id);
for (int i = 0; i < count; i++) {
if (trainers[i].id == id) {
for (int j = i; j < count - 1; j++) {
trainers[j] = trainers[j + 1];
}
count--;
found = 1;
printf("Record deleted.\n");
break;
}
}
if (!found) printf("Trainer not found.\n");
}
int main() {
int choice;
while (1) {
printf("\n1. Add Trainer\n2. List Trainers\n3. Modify Trainer\n4. Delete Trainer\n5. Exit\nEnter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: addTrainer(); break;
case 2: listTrainers(); break;
case 3: modifyTrainer(); break;
case 4: deleteTrainer(); break;
case 5: return 0;
default: printf("Invalid choice.\n");
}
}
return 0;
}
Assistant
Here's a detailed comparison and analysis of both solutions:
While Solution 1 works for basic functionality, the original version provides:
Professional data validation
Persistent storage
Better user experience
Robust error handling
Maintainable code structure
Recommendation: Use the original solution for production-grade applications, but Solution 1 could be acceptable for quick temporary use if enhanced with basic validation and file I/O.</count;>