Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help in editing the code under /display below. School year is stored in the struct as enum type. Need help converting enumtype for schooYear

Need help in editing the code under /display below.

School year is stored in the struct as enum type. Need help converting enumtype for schooYear back to display 'freshman','sophomore', 'junior' or 'senior' instead of 1, 2 , 3 , 4 when displaying. THANK YOU

#include #include #include #pragma warning(disable: 4996) // for Visual Studio Only

#define MAX_STUDENTS 15 #define MAX_NAME_LENGTH 25

typedef enum { freshman = 0, sophomore, junior, senior } schoolYear; // enum type

struct studentRecord { // struct for student details char studentName[MAX_NAME_LENGTH]; char major[MAX_NAME_LENGTH]; schoolYear schoolYear; unsigned int studentID; };

struct studentRecord list[MAX_STUDENTS]; // declare list of students int count = 0; // the number of students 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 addSort(char* studentName_input, char* major_input, char* schoolYear_input, unsigned int studentID_input); // 30 points void display(); // 10 points void load(char* fileName); // 10 points

int main() { char* fileName = "Student_List.txt"; load(fileName); // load list of students from file (if it exists). Initially there will be no file. char choice = 'i'; // initialized to a dummy value do { printf(" Enter your selection: "); printf("\t a: add a new student "); printf("\t d: display student list "); printf("\t q: quit "); choice = getchar(); flushStdIn(); executeAction(choice); } while (choice != 'q');

save(fileName); // save list of students to file (overwrites file, if it exists) return 0; }

// flush out leftover ' ' characters void flushStdIn() { char c; do c = getchar(); while (c != ' ' && c != EOF); }

// ask for details from user for the given selection and perform that action void executeAction(char c) { char studentName_input[MAX_NAME_LENGTH], major_input[MAX_NAME_LENGTH]; unsigned int studentID_input, add_result = 0; char schoolYear_input[20]; switch (c) { case 'a': // input student record from user printf(" Enter student name: "); fgets(studentName_input, sizeof(studentName_input), stdin); studentName_input[strlen(studentName_input) - 1] = '\0'; // discard the trailing ' ' char printf("Enter major: "); fgets(major_input, sizeof(major_input), stdin); major_input[strlen(major_input) - 1] = '\0'; // discard the trailing ' ' char

printf("Enter whether students is 'freshman' or 'sophomore' or 'junior' or 'senior': "); fgets(schoolYear_input, sizeof(schoolYear_input), stdin); schoolYear_input[strlen(schoolYear_input) - 1] = '\0'; // discard the trailing ' ' char printf("Please enter student ID number: "); scanf("%d", &studentID_input); flushStdIn();

// add the student to the list add_result = addSort(studentName_input, major_input, schoolYear_input, studentID_input); if (add_result == 0) printf(" Student is already on the list! "); else if (add_result == 1) printf(" Student successfully added to the list! "); else printf(" Unable to add. Student list is full! ");

break; case 'd': display(); break; case 'q': break; default: printf("%c is invalid input! ", c); } }

// addSort int addSort(char* studentName_input, char* major_input, char* schoolYear_input, unsigned int studentID_input) { for (int i = 0; i < count; i++) { if (strcmp(list[i].studentName, studentName_input) == 0) // check if student is on the list return 0; }

if (MAX_STUDENTS == count) // check if the list is full return 2;

// add student to list struct studentRecord type; strcpy(type.studentName, studentName_input); strcpy(type.major, major_input); type.studentID = studentID_input;

//convert the string 'schoolYear_input' to an enum type if (strcmp(schoolYear_input, "freshman") == 0) type.schoolYear = 0; else if (strcmp(schoolYear_input, "sophomore") == 0) type.schoolYear = 1; else if (strcmp(schoolYear_input, "junior") == 0) type.schoolYear = 2; else if (strcmp(schoolYear_input, "senior") == 0) type.schoolYear = 3;

list[count] = type; // set struct to the struct of what is being added count++; // increment by the student added to list for (int i = 0; i < count; i++) { // to sort student list for (int j = 0; j < count - 1; j++) { if (strcmp(list[j].studentName, list[j + 1].studentName) > 0) { struct studentRecord temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } return 1;

}

//****HELP WITH THIS PART***** // display void display() {

if (count == 0) { //check if list is empty printf("No students on the list. "); } else { printf("Student List: "); // iterate through list and print stored info for (int i = 0; i < count; i++) {

printf("Name: %s, Major: %s, Year: %d, ID: %d ", list[i].studentName, list[i].major, list[i].schoolYear, list[i].studentID); } } }

// 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 student 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\\Documents\Visual Studio 2019\Projects\Project1\Project1 // or // C:\Users\\source epos\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, schoolYearValue = 0; file = fopen(fileName, "wb"); // open file for writing

fwrite(&count, sizeof(count), 1, file); // First, store the number of students in the list

// Parse the list and write student record to file // for (i = 0; i < count; i++) { fwrite(list[i].studentName, sizeof(list[i].studentName), 1, file); fwrite(list[i].major, sizeof(list[i].major), 1, file); // convert enum to a number for storing if (list[i].schoolYear == freshman) schoolYearValue = 0; // 0 for freshman else if (list[i].schoolYear == sophomore) schoolYearValue = 1; // 1 for sophomore else if (list[i].schoolYear == junior) schoolYearValue = 2; // 2 for junior else schoolYearValue = 3; // 3 for senior

fwrite(&schoolYearValue, sizeof(schoolYearValue), 1, file); fwrite(&list[i].studentID, sizeof(list[i].studentID), 1, file); }

fclose(file); // close the file after writing }

// Q3: load (10 points) // This function is called in the beginning of main(). // This function reads the student 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 "Student_List.txt not found" because the file did not exist initially.) // If the file exists, then parse the student list to read the student 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) { FILE* file = fopen(fileName, "rb"); // check if file exists if (file == NULL) { printf("File not found. "); } else { char studentName[MAX_NAME_LENGTH], major[MAX_NAME_LENGTH], schoolYear[20]; unsigned int studentID; // read data from file and add to list while (fscanf(file, "%s %s %s %d", studentName, major, schoolYear, &studentID) != EOF) { addSort(studentName, major, schoolYear, studentID); } // close file fclose(file); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

More Books

Students also viewed these Databases questions

Question

Develop successful mentoring programs. page 418

Answered: 1 week ago