Hello! I'm posting this program that is partially completed if someone can help me out, I will give you a good rating!
Thanks,
// You are given a partially completed program that creates a list of employees,
like employees' record.
// Each record has this information: employee's name, supervisors's name,
department of the employee, room number.
// The struct 'employeeRecord' holds information of one employee. Department is
enum type.
// An array of structs called 'list' is made to hold the list of employees.
// To begin, you should trace through the given code and understand how it works.
// Please read the instructions above each required function and follow the
directions carefully.
// You should not modify any of the given code, the return types, or the
parameters, you risk getting compile error.
// You are not allowed to modify main ().
// You can use string library functions.
// WRITE COMMENTS FOR IMPORANT STEPS IN YOUR CODE.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable: 4996) // for Visual Studio Only
#define MAX_EMPLOYEES 15
#define MAX_NAME_LENGTH 25
typedef enum { HR = 0, Marketing, IT } departmentType; // enum type
struct employeeRecord { // struct for emplyee details
char employeeName[MAX_NAME_LENGTH];
char supervisorName[MAX_NAME_LENGTH];
departmentType department;
unsigned int roomNumber;
};
struct employeeRecord list[MAX_EMPLOYEES]; // declare list of employees
int count = 0;
// the number of employees
currently stored in the list (initialized to 0)
// functions already implmented
void flushStdIn();
void executeAction(char);
void save(char* fileName);
// functions that need implementation:
int add(char* employeeName_input, char* supervisorName_input, char*
department_input, unsigned int roomNumber_input); // 20 points
void display(); // 10 points
void sort(); // 10 points
void load(char* fileName); // 10 points
int main()
{
char* fileName = "Employee_List.txt";
load(fileName); // load list of employees from file (if it exists).
Initially there will be no file.
char choice = 'i'; // initialized to a dummy value
do
{
printf("\nEnter your selection:\n");
printf("\t a: add a new employee\n");
printf("\t d: display employee list\n");
printf("\t s: sort employee list by name\n");
printf("\t q: quit\n");
choice = getchar();
flushStdIn();
executeAction(choice);
} while (choice != 'q');
save(fileName); // save list of employees to file (overwrites file, if it
exists)
return 0;
}
// flush out leftover '\n' characters
void flushStdIn()
{
char c;
do c = getchar();
while (c != '\n' && c != EOF);
}
// ask for details from user for the given selection and perform that action
void executeAction(char c)
{
char employeeName_input[MAX_NAME_LENGTH],
supervisorName_input[MAX_NAME_LENGTH];
unsigned int roomNumber_input, add_result= 0;
char department_input[20];
switch (c)
{
case 'a':
// input employee record from user
printf("\nEnter employee name: ");
fgets(employeeName_input, sizeof(employeeName_input), stdin);
employeeName_input[strlen(employeeName_input) - 1] = '\0'; // discard
the trailing '\n' char
printf("Enter supervisor name: ");
fgets(supervisorName_input, sizeof(supervisorName_input), stdin);
supervisorName_input[strlen(supervisorName_input) - 1] = '\0'; //
discard the trailing '\n' char
printf("Enter whether employee is in 'HR' or 'Marketing' or 'IT': ");
fgets(department_input, sizeof(department_input), stdin);
department_input[strlen(department_input) - 1] = '\0'; // discard
the trailing '\n' char
printf("Please enter room number: ");
scanf("%d", &roomNumber_input);
flushStdIn();
// add the employee to the list
add_result = add(employeeName_input, supervisorName_input,
department_input, roomNumber_input);
if (add_result == 0)
printf("\nEmployee is already on the list! \n\n");
else if (add_result == 1)
printf("\nEmployee successfully added to the list! \n\n");
else
printf("\nUnable to add. Employee list is full! \n\n");
break;
case 'd': display(); break;
case 's': sort(); break;
case 'q': break;
default: printf("%c is invalid input!\n",c);
}
}
// Q1 : add (20 points)
// This function is used to add a employee into the list. You can simply add the
new employee to the end of list (array of structs).
// Do not allow the employee to be added to the list if it already exists in the
list. You can do that by checking employee names already in the list.
// If the employee already exists then return 0 without adding it to the list. If
the employee does not exist in the list then go on to add the employee at the end
of the list and return 1.
// If employee list is full, then do not add new employee to the list and return 2.
// NOTE: Notice how return type of add() is checked in case 'a' of executeAction()
// NOTE: You must convert the string 'department_input' to an enum type and store
it in the list because the department has enum type (not string type).
// The list should be case sensitive. For instance, 'Roger' and 'roger' should be
considered two different names.
// Hint: 'count' holds the number of employees currently in the list
int add(char* employeeName_input, char* supervisorName_input, char*
department_input, unsigned int roomNumber_input)
{
return 0; // edit this line as needed
}
// Q2 : display (10 points)
// This function displays the employee list with the details (struct elements) of
each employee.
// Parse through the list and print the employee details one after the other. See
expected output screenshots in question pdf file.
// NOTE: Department is stored in the struct as enum type. You need to display
'HR','Marketing' or 'IT'
void display()
{
}
// Q3 : sort (10 points)
// This function is used to sort the list(array of structs) alphabetically by
employee name.
// Parse the list and compare the employee names to check which one should appear
before the other in the list.
// Sorting should happen within the list. That is, you should not create a new
array of structs having sorted employees.
// Hint: One of the string library function can be useful to implement this
function because the sorting needs to happen by employee name which is a string.
// Use a temp struct (already declared) if you need to swap two structs in
your logic
void sort()
{
struct employeeRecord employeeTemp; // needed for swapping structs. Not
absolutely necessary to use.
// enter code here
// display message for user to check the result of sorting.
printf("\nEmployee list sorted! Use display option 'd' to view sorted list.\
n");
}
// save() is called at the end of main()
// This function saves the array of structures to file. It is already implemented.
// You should read and understand how this code works. It will help you with
'load()' function.
// save() is called at end of main() to save the employee list to a file.
// The file is saved at the same place as your C file. For VS, the default
directory looks like this:
// C:\Users\<username>\Documents\Visual Studio 2017\Projects\Project1\Project1
// You can simply delete the file to 'reset the list' or to avoid loading from it.
void save(char* fileName)
{
FILE* file;
int i, departmentValue=0;
file = fopen(fileName, "wb"); // open file for writing
fwrite(&count, sizeof(count), 1, file); // First, store the number of
employees in the list
// Parse the list and write employee record to file
//
for (i = 0; i < count; i++)
{
fwrite(list[i].employeeName, sizeof(list[i].employeeName), 1, file);
fwrite(list[i].supervisorName, sizeof(list[i].supervisorName), 1,
file);
// convert enum to a number for storing
if (list[i].department == HR)
departmentValue = 0; // 0 for HR
else if (list[i].department == Marketing)
departmentValue = 1; // 1 for Marketing
else
departmentValue = 2; // 2 for IT
fwrite(&departmentValue, sizeof(departmentValue), 1, file);
fwrite(&list[i].roomNumber, sizeof(list[i].roomNumber), 1, file);
}
fclose(file); // close the file after writing
}
// Q4: load (10 points)
// This function is called in the beginning of main().
// This function reads the employee list from the saved file and builds the array
of structures 'list'.
// In the first run of the program, there will be no saved file because save() is
called at the end of program.
// So at the begining of this function, write code to open the file and check if it
exists. If file does not exist, then return from the function.
// (See expected output of add() in homework question file. It displays
"Employee_List.txt not found" because the file did not exist initially.)
// If the file exists, then parse the employee list to read the employee details
from the file.
// Use the save function given above as an example of how to write this function.
Notice the order in which the struct elements are saved in save()
// You need to use the same order to read the list back.
// NOTE: The saved file is not exactly readable because all elements of the struct
are not string or char type.
// So you need to implement load() similar to how save() is implemented. Only
then the 'list' will be loaded correctly.
// You can simply delete the file to 'reset the list' or to avoid loading
from it.
void load(char* fileName)
{
Code Image:
Sample Output:
Code to Copy:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable: 4996) // for Visual Studio Only
#define MAX_EMPLOYEES 15
#define MAX_NAME_LENGTH 25
typedef enum { HR = 0, Marketing, IT } departmentType; // enum type
struct employeeRecord { // struct for emplyee details
char employeeName[MAX_NAME_LENGTH];
char supervisorName[MAX_NAME_LENGTH];
departmentType department;
unsigned int roomNumber;
};
struct employeeRecord list[MAX_EMPLOYEES]; // declare list of employees
int count = 0;
// the number of employees
//currently stored in the list(initialized to 0)
// functions already implmented
void flushStdIn();
void executeAction(char);
void save(char* fileName);
// functions that need implementation:
int add(char* employeeName_input, char* supervisorName_input, char*
department_input, unsigned int roomNumber_input); // 20 points
void display(); // 10 points
void sort(); // 10 points
void load(char* fileName); // 10 points
int main() {
char fileName[] = "Employee_List.txt";
load(fileName); // load list of employees from file (if it exists).Initially there will be no file.
char choice = 'i'; // initialized to a dummy value
do {
printf("\nEnter your selection:\n");
printf("\t a: add a new employee\n");
printf("\t d: display employee list\n");
printf("\t s: sort employee list by name\n");
printf("\t q: quit\n");
choice = getchar();
flushStdIn();
executeAction(choice);
} while (choice != 'q');
save(fileName); // save list of employees to file (overwrites file, if itexists)
return 0;
}
//Implementation of flusStdIn function
//flush out leftover '\n' characters
void flushStdIn() {
char c;
do c = getchar();
while (c != '\n' && c != EOF);
}
//Implementation of executeAction function
//ask for details from user for the given selection and perform that action
//Implementation of executeAction function
void executeAction(char c) {
char employeeName_input[MAX_NAME_LENGTH], supervisorName_input[MAX_NAME_LENGTH];
unsigned int roomNumber_input, add_result = 0;
char department_input[20];
switch (c) {
case 'a':
// input employee record from user
printf("\nEnter employee name: ");
//call fgets function
fgets(employeeName_input, sizeof(employeeName_input), stdin);
employeeName_input[strlen(employeeName_input) - 1] = '\0'; // discardthe trailing '\n' char
//Display statement
printf("Enter supervisor name: ");
//call fgets function
fgets(supervisorName_input, sizeof(supervisorName_input), stdin);
supervisorName_input[strlen(supervisorName_input) - 1] = '\0'; //discard the trailing '\n' char
//Display statement
printf("Enter whether employee is in 'HR' or 'Marketing' or 'IT': ");
//call fgets function
fgets(department_input, sizeof(department_input), stdin);
department_input[strlen(department_input) - 1] = '\0'; // discard the trailing '\n' char
//Display statement
printf("Please enter room number: ");
scanf("%d", &roomNumber_input);
flushStdIn();
// add the employee to the list
add_result = add(employeeName_input, supervisorName_input, department_input, roomNumber_input);
if (add_result == 0)
//Display statement
printf("\nEmployee is already on the list! \n\n");
else if (add_result == 1)
//Display statement
printf("\nEmployee successfully added to the list! \n\n");
else
//Display statement
printf("\nUnable to add. Employee list is full! \n\n");
break;
case 'd': display(); break;
case 's': sort(); break;
case 'q': break;
default:
//Display statement
printf("%c is invalid input!\n", c);
}
}
// Q1 : add (20 points)
// This function is used to add a employee into the list. You can simply add the
//new employee to the end of list(array of structs).
// Do not allow the employee to be added to the list if it already exists in the
//list.You can do that by checking employee names already in the list.
// If the employee already exists then return 0 without adding it to the list. If
//the employee does not exist in the list then go on to add the employee at the end
//of the list and return 1.
// If employee list is full, then do not add new employee to the list and return 2.
// NOTE: Notice how return type of add() is checked in case 'a' of executeAction()
// NOTE: You must convert the string 'department_input' to an enum type and store
//it in the list because the department has enum type(not string type).
// The list should be case sensitive. For instance, 'Roger' and 'roger' should be
//considered two different names.
// Hint: 'count' holds the number of employees currently in the list
//Implementation o
int add(char* employeeName_input, char* supervisorName_input, char*
department_input, unsigned int roomNumber_input)
{
//Declare k as type of integer
int k;
//check count is greater than equal to MAX_EMPLOYEE
if (count >= MAX_EMPLOYEES) {
return 2;
}
//Initialize k with 0
k = 0;
//Iterate the loop
while (k < count) {
//compare list[k].employeeName, employeeName_input
//is equal to zero
if (strcmp(list[k].employeeName, employeeName_input) == 0)
{
return 0;
}
//increment k
k++;
}
//call strcpy function with parameters
strcpy(list[k].employeeName, employeeName_input);
//call strcpy function with parameters
strcpy(list[k].supervisorName, supervisorName_input);
list[k].roomNumber = roomNumber_input;
//check department_input is equal to HR
if (strcmp(department_input, "HR") == 0) {
list[k].department = HR;
}
//check department_input is equal to Marketing
else if (strcmp(department_input, "Marketing") == 0) {
list[k].department = Marketing;
}
//check department_input is equal to IT
else if (strcmp(department_input, "IT") == 0) {
list[k].department = IT;
}
//increment the count
count = count + 1;
return 1; // edit this line as needed
}
// Q2 : display (10 points)
// This function displays the employee list with the details (struct elements) ofeach employee.
// Parse through the list and print the employee details one after the other. See
//expected output screenshots in question pdf file.
// NOTE: Department is stored in the struct as enum type. You need to display
//'HR', 'Marketing' or 'IT'
//Implementation of display function
void display()
{
//Declare k as type of integer
int k;
//Display statement for Employee name, Supervisor name, Department, Room number
printf("Employee name, Supervisor name, Department, Room number\n");
//Iterate the loop
for (k =0;k < count;k++)
{
//Display statement
printf("%s, %s, ", list[k].employeeName, list[k].supervisorName);
//check list[k].department is equal to HR
if (list[k].department == HR)
{
//Display statement
printf("HR, ");
}
//check list[k].department is equal to Marketing
else if (list[k].department == Marketing)
{
//Display statement
printf("Marketing, ");
}
//check list[k].department is equal to IT
else if (list[k].department == IT)
{
//Display statement
printf("IT, ");
}
//Display statement
printf("%d\n", list[k].roomNumber);
}
}
// Q3 : sort (10 points)
// This function is used to sort the list(array of structs) alphabetically by employee name.
// Parse the list and compare the employee names to check which one should appear
//before the other in the list.
// Sorting should happen within the list. That is, you should not create a new
//array of structs having sorted employees.
// Hint: One of the string library function can be useful to implement this
//function because the sorting needs to happen by employee name which is a string.
// Use a temp struct (already declared) if you need to swap two structs inyour logic
//Implementation of sort function
void sort()
{
struct employeeRecord tempRec; // needed for swapping structs. Notabsolutely necessary to use.
// enter code here
//Declare i and k as type of integer
int i, k;
//Initialize i with zero
i = 0;
//Iterate the loop
while (i < count)
{
//Iterate the loop
for (k = i + 1; k < count; k++)
{
//check and compare list[i].employeeName, list[k].employeeName is greater than zero or not
//then swap the elements
if (strcmp(list[i].employeeName, list[k].employeeName) > 0)
{
tempRec = list[i];
list[i] = list[k];
list[k] = tempRec;
}
}
//increment i
i++;
}
// display message for user to check the result of sorting.
printf("\nEmployee list sorted! Use display option 'd' to view sorted list.\n");
}
// save() is called at the end of main()
// This function saves the array of structures to file. It is already implemented.
// You should read and understand how this code works. It will help you with'load()' function.
// save() is called at end of main() to save the employee list to a file.
// The file is saved at the same place as your C file. For VS, the default directory looks like this:
// C:\Users\<username>\Documents\Visual Studio 2017\Projects\Project1\Project1
// You can simply delete the file to 'reset the list' or to avoid loading from it.
//Implementation of save function
void save(char* fileName)
{
//Declare file as type of FILE
FILE* file;
//Declare i , departmentValue as type of integer
int i, departmentValue = 0;
file = fopen(fileName, "w"); // open file for writing
fwrite(&count, sizeof(count), 1, file); // First, store the number ofemployees in the list
// Parse the list and write employee record to file
//Iterate the loop
for (i = 0; i < count; i++)
{
//call fwrite function
fwrite(list[i].employeeName, sizeof(list[i].employeeName), 1, file);
//call fwrite function
fwrite(list[i].supervisorName, sizeof(list[i].supervisorName), 1, file);
// convert enum to a number for storing
if (list[i].department == HR)
departmentValue = 0; // 0 for HR
else if (list[i].department == Marketing)
departmentValue = 1; // 1 for Marketing
else
departmentValue = 2; // 2 for IT
//call fwrite function
fwrite(&departmentValue, sizeof(departmentValue), 1, file);
//call fwrite function
fwrite(&list[i].roomNumber, sizeof(list[i].roomNumber), 1, file);
}
fclose(file); // close the file after writing
}
// Q4: load (10 points)
// This function is called in the beginning of main().
// This function reads the employee list from the saved file and builds the array of structures 'list'.
// In the first run of the program, there will be no saved file because save() is
//called at the end of program.
// So at the begining of this function, write code to open the file and check if it
//exists.If file does not exist, then return from the function.
// (See expected output of add() in homework question file. It displays
//"Employee_List.txt not found" because the file did not exist initially.)
// If the file exists, then parse the employee list to read the employee details from the file.
//Use the save function given above as an example of how to write this function.
//Notice the order in which the struct elements are saved in save()
// You need to use the same order to read the list back.
// NOTE: The saved file is not exactly readable because all elements of the struct
//are not string or char type.
// So you need to implement load() similar to how save() is implemented. Only
//then the 'list' will be loaded correctly.
// You can simply delete the file to 'reset the list' or to avoid loadingfrom it.
//Implementation of load function
void load(char* fileName)
{
//Declare file as type of FILE
FILE* file;
//Declare i, deptValue as type of integer
int k, deptValue = 0;
//open the file in read mode
file = fopen(fileName, "r");
//check file contains NULL or not
if (file == NULL)
{
//Display statement
printf("File not exist!!\n");
//exit the if condition
exit(0);
}
fread(&count, sizeof(count), 1, file);
//Initialize k with 0
k = 0;
//Iterate the loop
while (k < count)
{
//call fread function
fread(list[k].employeeName, sizeof(list[k].employeeName), 1, file);
//call fread function
fread(list[k].supervisorName, sizeof(list[k].supervisorName), 1, file);
//call fread function
fread(&deptValue, sizeof(deptValue), 1, file);
// convert the enum into to a number for storing the records
//check 0 means department is HR
if (deptValue == 0)
{
//Assign HR to list[k].department
list[k].department = HR;
}
//check 1 means department is HR
else if (deptValue == 1)
{
//Assign Marketing to list[k].department
list[k].department = Marketing;
}
else
{
//Assign IT to list[k].departement
list[k].department = IT;
}
//call fread function
fread(&list[k].roomNumber, sizeof(list[k].roomNumber), 1, file);
//increment k
k++;
}
//close the file
fclose(file);
}
Hello! I'm posting this program that is partially completed if someone can help me out, I...
I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves the array of structures to file. It is already implemented for you. // You should understand how this code works so that you know how to use it for future assignments. void save(char* fileName) { FILE* file; int i; file = fopen(fileName, "wb"); fwrite(&count, sizeof(count), 1, file); for (i = 0; i < count; i++) { fwrite(list[i].name, sizeof(list[i].name), 1, file); fwrite(list[i].class_standing, sizeof(list[i].class_standing), 1, file);...
// READ BEFORE YOU START: // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, gender, class, standard, and roll_number. // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow the directions carefully. // If you modify any of the given code, the return types, or the parameters, you...
// Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given a partially completed program that creates a list of patients, like patients' record. // Each record has this information: patient's name, doctor's name, critical level of patient, room number. // The struct 'patientRecord' holds information of one patient. Critical level is enum type. // An array of structs called 'list' is made to hold the list of patients. // To begin, you should trace...
Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...
Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...
Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...
Hardware Inventory – Write a database to keep track of tools, their cost and number. Your program should initialize hardware.dat to 100 empty records, let the user input a record number, tool name, cost and number of that tool. Your program should let you delete and edit records in the database. The next run of the program must start with the data from the last session. This is my program so far, when I run the program I keep getting...
Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...
IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...
// C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList { struct patient *patient; struct patientList *next; } *list = NULL; ...