Multi file Program
Question 1:
An HR management system needs to store and display employee salary details. The program
should allow the user to save an employee's name and salary to a file called [Link]. When
requested, it should display the stored employee names and salaries. Implement this using
multiple files for organization.
Tasks
Saving Employee Salary: When saving an employee's salary, ensure that the
employee's name
and salary are correctly stored in the [Link] file.
Displaying Employee Salary: The system should display the employee's name and
salary when
the user requests it.
Header File (employee.h)
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
void save_employee_salary(const char *name, float salary);
void display_employee_salaries();
#endif // EMPLOYEE_H
Source File for Employee Functions (employee.c)
// employee.c
#include <stdio.h>
#include <string.h>
#include "employee.h"
// Function to save employee name and salary to [Link]
void save_employee_salary(const char *name, float salary) {
FILE *file = fopen("[Link]", "a"); // Open the file in append mode
if (file == NULL) {
printf("Error opening file to save employee salary.\n");
return;
}
fprintf(file, "%s %.2f\n", name, salary); // Write employee name and salary to file
fclose(file); // Close the file
}
// Function to display all employee names and salaries stored in [Link]
void display_employee_salaries() {
FILE *file = fopen("[Link]", "r"); // Open the file in read mode
if (file == NULL) {
printf("No employee data found.\n");
return;
}
char name[100];
float salary;
printf("\nEmployee Salary Details:\n");
while (fscanf(file, "%s %f", name, &salary) != EOF) {
printf("Name: %s, Salary: %.2f\n", name, salary); // Display employee name and salary
}
fclose(file); // Close the file
}
Main Program (main.c)
#include <stdio.h>
#include <string.h>
#include "employee.h"
int main() {
int choice;
char name[100];
float salary;
while (1) {
printf("\nHR Management System\n");
printf("1. Save Employee Salary\n");
printf("2. Display Employee Salaries\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
// Save employee salary
printf("Enter employee name: ");
scanf("%s", name); // Take employee name as input
printf("Enter employee salary: ");
scanf("%f", &salary); // Take employee salary as input
save_employee_salary(name, salary); // Save employee data to the file
printf("Employee salary saved successfully.\n");
break;
case 2:
// Display employee salaries
display_employee_salaries(); // Display employee data from the file
break;
case 3:
// Exit the program
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Question 2:
A library management system needs to store and display book records. The program should
allow the user to save a book's title and ISBN to a file called [Link]. When requested, it
should display the stored books' title and ISBN. Implement this using multiple files for better
organization.
Tasks
Saving a Book Record: The system should store the book title and ISBN number in a
file called
[Link]. When saving a book, the title and ISBN should be correctly written to the
file.
Displaying Book Records: When the user requests to view a book record, the system
should
correctly display the stored book title and ISBN number.
Header File (book.h)
#ifndef BOOK_H
#define BOOK_H
void save_book_record(const char *title, const char *isbn);
void display_book_records();
#endif // BOOK_H
.Source File (book.c)
#include <stdio.h>
#include <string.h>
#include "book.h"
void save_book_record(const char *title, const char *isbn) {
FILE *file = fopen("[Link]", "a"); // Open the file in append mode
if (file == NULL) {
printf("Error opening file to save book record.\n");
return;
}
fprintf(file, "Title: %s, ISBN: %s\n", title, isbn); // Write book title and ISBN to the file
fclose(file); // Close the file
}
// Function to display all saved book records from [Link]
void display_book_records() {
FILE *file = fopen("[Link]", "r"); // Open the file in read mode
if (file == NULL) {
printf("No book records found.\n");
return;
}
char title[100];
char isbn[20];
printf("\nBook Records:\n");
while (fscanf(file, "Title: %99[^\n], ISBN: %19[^\n]\n", title, isbn) != EOF) {
printf("Title: %s, ISBN: %s\n", title, isbn); // Display book title and ISBN
}
fclose(file); // Close the file
}
Main Program (main.c)
#include <stdio.h>
#include <string.h>
#include "book.h"
int main() {
int choice;
char title[100], isbn[20];
while (1) {
// Display menu options to the user
printf("\nLibrary Management System\n");
printf("1. Save Book Record\n");
printf("2. Display Book Records\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
// Save book record
printf("Enter book title: ");
getchar(); // Consume newline left by previous scanf
fgets(title, sizeof(title), stdin); // Read the title of the book
title[strcspn(title, "\n")] = '\0'; // Remove trailing newline
printf("Enter book ISBN: ");
fgets(isbn, sizeof(isbn), stdin); // Read the ISBN
isbn[strcspn(isbn, "\n")] = '\0'; // Remove trailing newline
save_book_record(title, isbn); // Save the book record to the file
printf("Book record saved successfully.\n");
break;
case 2:
// Display all book records
display_book_records(); // Display book records from the file
break;
case 3:
// Exit the program
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Question 3:
Sophia, a budding programmer, was tasked with building a calculator program for a school
project. She decided to create a user-defined library (calculator.c) containing functions for
basic operations: addition, subtraction, multiplication, and division. Then, she wrote a
main.c program to interact with users and perform calculations using the library.
Tasks
Create the Calculator Library: Implement a separate file (calculator.c) with functions
for basic
arithmetic operations (addition, subtraction, multiplication, and division), and
declare them in a header file (calculator.h).
Develop the Main Program: Implement a main.c program that takes user input for
numbers and
operation type, and uses the functions from the calculator library to perform the
corresponding calculations.
. Header File (calculator.h)
#ifndef CALCULATOR_H
#define CALCULATOR_H
double add(double num1, double num2);
double subtract(double num1, double num2);
double multiply(double num1, double num2);
double divide(double num1, double num2);
#endif // CALCULATOR_H
Calculator Library (calculator.c)
#include <stdio.h>
#include "calculator.h"
double add(double num1, double num2) {
return num1 + num2;
}
double subtract(double num1, double num2) {
return num1 - num2;
}
double multiply(double num1, double num2) {
return num1 * num2;
}
double divide(double num1, double num2) {
if (num2 == 0) {
printf("Error: Division by zero is not allowed.\n");
return -1; // Return a special value for division by zero
}
return num1 / num2;
}
Main Program (main.c)
#include <stdio.h>
#include "calculator.h"
int main() {
double num1, num2;
int choice;
double result;
while (1) {
// Display the menu to the user
printf("\nSimple Calculator\n");
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("5. Exit\n");
printf("Enter your choice (1-5): ");
scanf("%d", &choice);
if (choice == 5) {
// Exit the program
printf("Exiting calculator. Goodbye!\n");
break;
}
// Prompt user for two numbers
printf("Enter the first number: ");
scanf("%lf", &num1);
printf("Enter the second number: ");
scanf("%lf", &num2);
// Perform the operation based on the user's choice
switch (choice) {
case 1:
result = add(num1, num2);
printf("Result: %.2lf\n", result);
break;
case 2:
result = subtract(num1, num2);
printf("Result: %.2lf\n", result);
break;
case 3:
result = multiply(num1, num2);
printf("Result: %.2lf\n", result);
break;
case 4:
result = divide(num1, num2);
if (result != -1) {
printf("Result: %.2lf\n", result);
}
break;
default:
printf("Invalid choice! Please enter a valid option (1-5).\n");
}
}
return 0;
}