In C language create a \'database\' that stores Employee information. The progra
ID: 3668595 • Letter: I
Question
In C language create a 'database' that stores Employee information. The program will read information from a file, and display certain information to the user, based on what they select.
Requirements:
Read employee information from a file (Format below)
Read filename from command line argument
Use functions
After reading in the file, print out a menu and take the appropriate actions...
Print a record based on employee ID
Print ALL employee records
Print all employees in a specific department Quit option
This menu should keep running so the user can run multiple options
until they choose to quit.
Store employee information in structures (an array of structures)
File Format:
Row 1: Number of employees
Rows 2+: employeeId firstName lastName departmentNumber salary
2
5 Nate Wilson 10 57473 8 Mike Brady 10 48999
SAMPLE OUTPUT:
Welcome to the Employee Database! Choose an option: 1: Print empid 2: Print ALL employees 3: Show all employees in department -1: QUIT 3 which department number? 10 ID: First Name: Nate Last Name: Wilson Department: 10 Salary: 57473.80e880 5 Salary: 57473.88888 ID: First Name: Mike Last Name: Brady Department: 10 Salary: 48999.0080ed 8Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
// Employee structure to store information
struct Employee {
int id;
char firstName[100];
char lastName[100];
int departmentNumber;
double salary;
} Employees[1000];
// to store number of employees added to database
int NUMBER_OF_EMPLOYEES;
// helper function to print employee
void printEmployee(struct Employee e) {
printf("ID: %d ", e.id);
printf("First Name: %s ", e.firstName);
printf("Last Name: %s ", e.lastName);
printf("Department: %d ", e.departmentNumber);
printf("Salary: %lf ", e.salary);
}
void printAllEmployees() {
int i;
for (i = 0; i < NUMBER_OF_EMPLOYEES; ++i) {
printEmployee(Employees[i]);
}
}
void printEmployeeById(int id) {
int i;
for (i = 0; i < NUMBER_OF_EMPLOYEES; ++i) {
if (Employees[i].id == id) {
printEmployee(Employees[i]);
return;
}
}
// no employee with given id
printf("Employee with given id %d not available ", id);
}
void printEmployeesByDepartment(int departmentNumber) {
int numberOfEmployeesFound = 0;
int i;
for (i = 0; i < NUMBER_OF_EMPLOYEES; ++i) {
if (Employees[i].departmentNumber == departmentNumber) {
printEmployee(Employees[i]);
++numberOfEmployeesFound;
}
}
// no employee in given department
if (numberOfEmployeesFound == 0) {
printf("No employee available in given department with id %d ", departmentNumber);
}
}
int main(int argc, char* argv[]) {
// read employee data from database
FILE *fp = fopen(argv[1], "r");
fscanf(fp, "%d", &NUMBER_OF_EMPLOYEES);
int i;
for (i = 0; i < NUMBER_OF_EMPLOYEES; ++i) {
fscanf(fp, "%d %s %s %d %lf",
&Employees[i].id,
Employees[i].firstName,
Employees[i].lastName,
&Employees[i].departmentNumber,
&Employees[i].salary);
}
printf(" Welcome to the employee database ");
printf("-------------------------------- ");
// set quit false
int QUIT = 0;
while (!QUIT) {
// print menu
printf("Choose an option ");
printf("1: Print empid ");
printf("2: Print ALL employees ");
printf("3: Show all employees in department ");
printf("-1: QUIT ");
int input, id;
scanf("%d", &input);
switch(input) {
case 1:
printf("Which employee id? ");
scanf("%d", &id);
printEmployeeById(id);
break;
case 2:
printAllEmployees();
break;
case 3:
printf("Which department id? ");
scanf("%d", &id);
printEmployeesByDepartment(id);
break;
case -1:
QUIT = 1;
break;
default:
printf("Error: Invalid input please try again! ");
}
}
printf("Thank you! ");
return 0;
}
INPUT FILE
4
1 John Cena 12 100000000
2 Rock Johnson 12 100000000
3 Tom Cruise 13 100000000
4 Angelina Jolie 13 10000000