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, 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 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\n\n");
        printf("Student information\n\n");

        do
        {
                printf("Please enter your selection:\n");
                printf("\ta: add a new student to the list\n");
                printf("\ts: search for a student on the list\n");
                printf("\td: display list of students\n");
                printf("\tq: quit and save your list\n");
                ch = tolower(getchar());
                flush();
                branching(ch);
        } while (ch != 'q');

        save("Student_List.txt"); // save list of students to file (overwrite if it exists)
        return 0;
}

// consume leftover '\n' characters
void flush()
{
        int c;
        do c = getchar(); while (c != '\n' && 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!\n");
        }
}

// 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("\nPlease enter the student's information in the following format:\n");
                printf("\tname:gender:standard:roll_number:tuition_fee\n");
                
                fgets(input, sizeof(input), stdin);

                // discard '\n' chars attached to input
                input[strlen(input) - 1] = '\0';

                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("\nThat student is already on the list\n\n");
                else
                        printf("\nStudent added to list successfully\n\n");
        }
        else // c = 's'
        {
                printf("\nPlease enter the student's information in the following format:\n");
                printf("\tname:roll_number\n");
                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("\nThat student is not on the list\n\n");
                else
                        printf("\nStandard: %s\n\n", result);
        }
}

// Q1 : add (30 points)
// 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 (array).
// 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 students 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 (10 points)
// 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 student.
// 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("\nThere are no students on this list!\n\n");
        else {
                for (i = 0; i < count; i++)
                {
                        printf("\nName: %s\n", list[i].name);

                        if (list[i].genderValue == male)
                                genderValue = "Male";
                        else if (list[i].genderValue == female)
                                genderValue = "Female";

                        printf("Standard: %s\n", list[i].standard);
                        printf("Gender: %s\n", genderValue);
                        printf("Roll No: %d \n", list[i].roll_number);
                        printf("Tuition Fee: $ %.2f \n", list[i].tuition_fee);
                }
                printf("\n");
        }
}

// 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;
}

Example outputs for the use of these functions are included below.


add: Assignment5Array of Structs and Enum Types tudent inforrmation Please enter your selection: a: add a new student to the list s: search for a student on the list d: display list of students q: quit and save your list ai Please enter the students information in the following format: Tom:M:1st:11211:2350.75 Student added to list successfully Please enter your selection: name gender standard:roll_number tuition fee a: add a new student to the list s search for a student on the 1ist d: display list of students q: quit and save your list Please enter the students information in the following format: Jerry:M:1st :12321:2450.55 Student added to list successfully Please enter your selection: name gender:standard roll_number:tuition_fee a: add a new student to the list s: search for a student on the list d: display list of students q: quit and save you list ai Please enter the students information in the following format: Elsa:F:3rd:1233:3450.65 Student added to list successfully Please enter your selection: name gender standard:roll_number tuition fee a: add a new student to the list s search for a student on the 1ist d: display list of students q quit and save your list

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code for load() , search() and add() funtions . Please add in comments if anything else needed

int add(char* name, char* genderValueString, char* standard, int roll_number, float tuition_fee, struct student* list)
{
int i = 0, ret = 0 ,j=0;
int flag = 0;

if((roll_number < 0) || !(name[0] >=65 && name[0] <=90) || !(isdigit(standard[0])) || (genderValueString[0] != 'M' && genderValueString[0] != 'F') ){
return 0;
}

if(count != 0){

while((i < count) && ((ret = strcmp(list[i].name,name)) < 0)){
i++;
}

if(ret == 0){
if(list[i].roll_number == roll_number)
return 0;
else
i++;
}

if(i != count){
for(j=i ; j < count ;j++){
list[j+1] = list[j];
}
}
}

strncpy(list[i].name,name,sizeof(list[i].name) - 1);
list[i].name[29] = '\0';
if(!strncmp(genderValueString,"F",1)){
list[i].genderValue = female;
}
else{
list[i].genderValue = male;
}
  
strncpy(list[i].standard,standard,strlen(standard));
list[i].roll_number = roll_number;
  
list[i].tuition_fee = tuition_fee ;

count++;
  
return 1;
}

char* search(char* name, int roll_number, struct student* list)
{
int i =0;

for(i=0; i < count ;i++){
if( !(strcmp(name,list[i].name)) && (list[i].roll_number == roll_number))
return list[i].standard;
}

return NULL;
}

void load(char* fileName)
{
FILE* fp = fopen(fileName,"r");
char ch = ' ';
char gen[10] = {'\0'};
char input[200] = {'\0'};
int i = 0;

if(fp == NULL){
printf("Cannot open the file\n");
return;
}

i = count;
while(ch != EOF ){
fscanf(fp,"%[^\n]",input);

if(strlen(input) == 0)
break;

strcpy(list[i].name,strtok(input,":"));
  
strncpy(gen,strtok(NULL,":"),10);
if(gen[0] == 'M')
list[i].genderValue = male;
else
list[i].genderValue = female;

strncpy(list[i].standard,strtok(NULL,":"),sizeof(list[i].standard) - 1);
  
list[i].roll_number = atoi(strtok(NULL,":"));
list[i].tuition_fee = atof(strtok(NULL,":"));
  
i++;
count++;
ch = fgetc(fp);
memset(input,0,sizeof(input));
}

}

Add a comment
Know the answer?
Add Answer to:
// READ BEFORE YOU START: // You are given a partially completed program that creates a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves...

    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);...

  • // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given...

    // 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...

  • ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE...

    ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE ANSWERING CODING SECTIONS HW07 #Q1-Q5 HW08 #Q1-Q2 // READ BEFORE YOU START: // Please read the given Word document for the project description with an illustrartive diagram. // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, standard, and a linked list of absents. // Please read the instructions...

  • Hello! I'm posting this program that is partially completed if someone can help me out, I...

    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...

  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    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...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    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,...

  • // C code // If you modify any of the given code, the return types, or...

    // 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;  ...

  • Hardware Inventory – Write a database to keep track of tools, their cost and number. Your...

    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...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • Writing a program in C please help!! My file worked fine before but when I put...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT