// READ BEFORE YOU START: // You are given a partially completed program that cr
ID: 3793938 • Letter: #
Question
// 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, section, 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 risk failing the automated test cases. // // The following will be accepted as input in the following format: "name:gender:standard:roll_number:tuition_fee" // Example Input: "Tom:M:3rd:10:2000.10" or "Elsa:F:4th:15:2700" // Valid name: String containing alphabetical letters beginning with a capital letter // Valid gender: Char value 'M' or 'F' // Valid standard: String containing alpha-numeric letters beginning with a number // Valid roll_number: Positive integer value // Valid tuition fee: Float containing no more than 2 decimal value, for example: 1500.45 or 2000.7 or 2700 // All inputs will be a valid length and no more than the allowed number of students will be added to the list #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #pragma warning(disable: 4996) typedef enum { male = 0, female } gender; // enumeration type gender struct student { char name[30]; gender genderValue; char standard[10]; int roll_number; float tuition_fee; }; int count = 0; // the number of students currently stored in the list (initialized at 0) struct student list[30]; // initialize list of students // forward declaration of functions void flush(); void branching(char); void registration(char); int add(char*, char*, char*, int, float, struct student*); // 30 points char* search(char*, int, struct student*); // 10 points void display(); void save(char* fileName); void load(char* fileName); // 10 points int main() { load("Sudent_List.txt"); // load list of students from file (if it exists) char ch = 'i'; printf("Assignment 5: Array of Structs and Enum Types "); printf("Student information "); do { printf("Please enter your selection: "); printf(" a: add a new student to the list "); printf(" s: search for a student on the list "); printf(" d: display list of students "); printf(" q: quit and save your list "); ch = tolower(getchar()); flush(); branching(ch); } while (ch != 'q'); save("Student_List.txt"); // save list of dogs to file (overwrite if it exists) getch(); return 0; } // consume leftover ' ' characters void flush() { int c; do c = getchar(); while (c != ' ' && c != EOF); } // branch to different tasks void branching(char c) { switch (c) { case 'a': case 's': registration(c); break; case 'd': display(); break; case 'q': break; default: printf("Invalid input! "); } } // The registration function is used to determine how much information is needed and which function to send that information to. // It uses values that are returned from some functions to produce the correct ouput. // There is no implementation needed here, but you should study this function and know how it works. // It is always helpful to understand how the code works before implementing new features. // Do not change anything in this function or you risk failing the automated test cases. void registration(char c) { char input[100]; if (c == 'a') { printf(" Please enter the student's information in the following format: "); printf(" name:gender:standard:roll_number:tuition_fee "); fgets(input, sizeof(input), stdin); // discard ' ' chars attached to input input[strlen(input) - 1] = ''; char* name = strtok(input, ":"); // strtok used to parse string char* genderValueString = strtok(NULL, ":"); char* standard = strtok(NULL, ":"); int roll_number = atoi(strtok(NULL, ":")); // atoi used to convert string to int float tuition_fee = atof(strtok(NULL, ":")); // atof used to convert string to float int result = add(name, genderValueString, standard, roll_number, tuition_fee, list); if (result == 0) printf(" That student is already on the list "); else printf(" Student added to list successfully "); } else // c = 's' { printf(" Please enter the student's information in the following format: "); printf(" name:roll_number "); fgets(input, sizeof(input), stdin); char* name = strtok(input, ":"); // strtok used to parse string int roll_number = atoi(strtok(NULL, ":")); // atoi used to convert string to int char* result = search(name, roll_number, list); if (result == NULL) printf(" That student is not on the list "); else printf(" Standard: %s ", result); } } // Q1 : add (66% of specifications) // This function is used to insert a new student into the list. // Your list should be sorted alphabetically by name, so you need to search for the correct index to add into your list. // If a student already exists with the same name, then those students should be sorted by roll_number. // Do not allow for the same student to be added to the list multiple times. (same name and same roll_number(same roll number cannot happen)). // If the student already exists on the list, return 0. If the student is added to the list, return 1. // // NOTE: You must convert the string "genderValueString to an enum type and store it in the list. This will be tested. // (You must store all of the required information correctly to pass all of the test cases) // NOTE: You should not allow for the same student to be added twice, you will lose points if you do not account for this. // (That means that dogs on the list are allowed to have the same name but not same roll number). // You are not required to use pointer operations for your list but you may do so if you'd like. // 'list' is passed to this function for automated testing purposes only, it is global. int add(char* name, char* genderValueString, char* standard, int roll_number, float tuition_fee, struct student* list) { return 0; } // Q2 : search (33% of specifications) // This function is used to search for a student on the list and returns the standard of that student // You will need to compare the search keys: name and roll_number, with the stored name and roll_number. // If the student exists in the list, return a String containing the standard of the requested dog. // If the student does not exist on the list, return NULL char* search(char* name, int roll_number, struct student* list) { return NULL; } // This function displays the list of students and the information for each one. It is already implemented for you. // It may be helpful to trace through this function to help you complete other sections of this assignment. void display() { char* genderValue = "Male"; int i; if (count == 0) printf(" There are no students on this list! "); else { for (i = 0; i < count; i++) { printf(" Name: %s ", list[i].name); if (list[i].genderValue == male) genderValue = "Male"; else if (list[i].genderValue == female) genderValue = "Female"; printf("Standard: %s ", list[i].standard); printf("Gender: %s ", genderValue); printf("Roll No: %d ", list[i].roll_number); printf("Tuition Fee: $ %.2f ", list[i].tuition_fee); } printf(" "); } } // 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].standard, sizeof(list[i].standard), 1, file); fwrite(&list[i].genderValue, sizeof(list[i].genderValue), 1, file); fwrite(&list[i].roll_number, sizeof(list[i].roll_number), 1, file); fwrite(&list[i].tuition_fee, sizeof(list[i].tuition_fee), 1, file); } fclose(file); } // Q3: Load file (10 points) // This function loads data from file and build the the array of structures. // Use the save function given above as an example on how to write this function. void load(char* fileName) { return NULL; }
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#pragma warning(disable: 4996)
typedef enum { male = 0, female } gender; // enumeration type gender
struct student {
char name[30];
gender genderValue;
char standard[10];
int roll_number;
float tuition_fee;
};
int count = 0; // the number of students currently stored in the list (initialized at 0)
struct student list[30]; // initialize list of students
// forward declaration of functions
void flush();
void branching(char);
void registration(char);
int add(char*, char*, char*, int, float, struct student*); // 30 points
char* search(char*, int, struct student*); // 10 points
void display();
void save(char* fileName);
void load(char* fileName); // 10 points
int main()
{
load("Sudent_List.txt"); // load list of students from file (if it exists)
char ch = 'i';
printf("Assignment 5: Array of Structs and Enum Types ");
printf("Student information ");
do
{
printf("Please enter your selection: ");
printf(" a: add a new student to the list ");
printf(" s: search for a student on the list ");
printf(" d: display list of students ");
printf(" q: quit and save your list ");
ch = tolower(getchar());
flush();
branching(ch);
} while (ch != 'q');
save("Student_List.txt"); // save list of dogs to file (overwrite if it exists)
getchar();
return 0;
}
// consume leftover ' ' characters
void flush()
{
int c;
do c = getchar(); while (c != ' ' && c != EOF);
}
// branch to different tasks
void branching(char c)
{
switch (c)
{
case 'a':
case 's': registration(c); break;
case 'd': display(); break;
case 'q': break;
default: printf("Invalid input! ");
}
}
// The registration function is used to determine how much information is needed and which function to send that information to.
// It uses values that are returned from some functions to produce the correct ouput.
// There is no implementation needed here, but you should study this function and know how it works.
// It is always helpful to understand how the code works before implementing new features.
// Do not change anything in this function or you risk failing the automated test cases.
void registration(char c)
{
char input[100];
if (c == 'a')
{
printf(" Please enter the student's information in the following format: ");
printf(" name:gender:standard:roll_number:tuition_fee ");
fgets(input, sizeof(input), stdin);
// discard ' ' chars attached to input
input[strlen(input) - 1] = '';
char* name = strtok(input, ":"); // strtok used to parse string
char* genderValueString = strtok(NULL, ":");
char* standard = strtok(NULL, ":");
int roll_number = atoi(strtok(NULL, ":")); // atoi used to convert string to int
float tuition_fee = atof(strtok(NULL, ":")); // atof used to convert string to float
int result = add(name, genderValueString, standard, roll_number, tuition_fee, list);
if (result == 0)
printf(" That student is already on the list ");
else
printf(" Student added to list successfully ");
}
else // c = 's'
{
printf(" Please enter the student's information in the following format: ");
printf(" name:roll_number ");
fgets(input, sizeof(input), stdin);
char* name = strtok(input, ":"); // strtok used to parse string
int roll_number = atoi(strtok(NULL, ":")); // atoi used to convert string to int
char* result = search(name, roll_number, list);
if (result == NULL)
printf(" That student is not on the list ");
else
printf(" Standard: %s ", result);
}
}
// Q1 : add (66% of specifications)
// This function is used to insert a new student into the list.
// Your list should be sorted alphabetically by name, so you need to search for the correct index to add into your list.
// If a student already exists with the same name, then those students should be sorted by roll_number.
// Do not allow for the same student to be added to the list multiple times. (same name and same roll_number(same roll number cannot happen)).
// If the student already exists on the list, return 0. If the student is added to the list, return 1.
//
// NOTE: You must convert the string "genderValueString to an enum type and store it in the list. This will be tested.
// (You must store all of the required information correctly to pass all of the test cases)
// NOTE: You should not allow for the same student to be added twice, you will lose points if you do not account for this.
// (That means that dogs on the list are allowed to have the same name but not same roll number).
// You are not required to use pointer operations for your list but you may do so if you'd like.
// 'list' is passed to this function for automated testing purposes only, it is global.
int add(char* name, char* genderValueString, char* standard, int roll_number, float tuition_fee, struct student* list)
{
//If list is already full then can't add new Student
if(count >= 30){
printf("Can't add More than 30 students");
return 0;
}
struct student*curStudent = list+count;
//Copy name
memcpy(&(curStudent->name[0]),name,strlen(name));
//Copy Gender
if(*genderValueString == 'M'){
curStudent->genderValue = male;
}
else{
curStudent->genderValue = female;
}
//Copy Standard
memcpy(&(curStudent->standard[0]),standard,strlen(standard));
//Roll Number
curStudent->roll_number = roll_number;
//Tution Fee
curStudent->tuition_fee = tuition_fee;
//Increment Count
count++;
return 0;
}
// Q2 : search (33% of specifications)
// This function is used to search for a student on the list and returns the standard of that student
// You will need to compare the search keys: name and roll_number, with the stored name and roll_number.
// If the student exists in the list, return a String containing the standard of the requested dog.
// If the student does not exist on the list, return NULL
char* search(char* name, int roll_number, struct student* list)
{
for(int i = 0 ; i < count ; i++){
if(strcmp(list[i].name,name) == 0 && list[i].roll_number == roll_number){
return list[i].standard;
}
}
return NULL;
}
// This function displays the list of students and the information for each one. It is already implemented for you.
// It may be helpful to trace through this function to help you complete other sections of this assignment.
void display()
{
char* genderValue = "Male";
int i;
if (count == 0)
printf(" There are no students on this list! ");
else {
for (i = 0; i < count; i++)
{
printf(" Name: %s ", list[i].name);
if (list[i].genderValue == male)
genderValue = "Male";
else if (list[i].genderValue == female)
genderValue = "Female";
printf("Standard: %s ", list[i].standard);
printf("Gender: %s ", genderValue);
printf("Roll No: %d ", list[i].roll_number);
printf("Tuition Fee: $ %.2f ", list[i].tuition_fee);
}
printf(" ");
}
}
// 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].standard, sizeof(list[i].standard), 1, file);
fwrite(&list[i].genderValue, sizeof(list[i].genderValue), 1, file);
fwrite(&list[i].roll_number, sizeof(list[i].roll_number), 1, file);
fwrite(&list[i].tuition_fee, sizeof(list[i].tuition_fee), 1, file);
}
fclose(file);
}
// Q3: Load file (10 points)
// This function loads data from file and build the the array of structures.
// Use the save function given above as an example on how to write this function.
void load(char* fileName)
{
FILE *infile;
//Open file to read data
infile = fopen (fileName,"r");
//File open Failed
if(infile == NULL){
fprintf(stderr, " Error opening students data ");
return NULL;
}
//Temp variable to read Gender from File
char gen[2] = {''};
char test[200] = {''};
//Read from File : Format "Tom:M:3rd:10:2000.10"
while(fscanf(infile,"%s",test)!= EOF){
char* name = strtok(test, ":"); // strtok used to parse string
char* genderValueString = strtok(NULL, ":");
char* standard = strtok(NULL, ":");
int roll_number = atoi(strtok(NULL, ":")); // atoi used to convert string to int
float tuition_fee = atof(strtok(NULL, ":")); // atof used to convert string to float
add(name, genderValueString, standard, roll_number, tuition_fee, list);
}
fclose(infile);
return NULL;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.