2

I am studing for a test and have this question : Modify this program so that it takes input in a different format:each line consists of an age, a comma, a space and a name, e.g.

23, Angus McGurkinshaw

I understand I need to modify something in readOneStudent function.Not sure how to read the name by knowing the address of comma.Please help. The input and output should look like this:

input = 21, Fred Nurk
        927, Arwen Evensong
output is suppose to be:
        Arwen Evensong (927)
        Fred Nurk (21)

..

 #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>

    #define MAX_LINE_LENGTH 80      // The longest line this program will accept
    #define MAX_NUM_STUDENTS 500    // The maximum number of students this program can handle
    #define MAX_NAME_SIZE 50        // The maximum allowable name length

    typedef struct student_s Student;

    struct student_s {
        char name[MAX_NAME_SIZE];
        int age;
        Student* next;              // Pointer to next student in a list
    };

    // Create a pool of student records to be allocated on demand

    Student studentPool[MAX_NUM_STUDENTS];  // The student pool
    int firstFree = 0;

    // Return a pointer to a new student record from the pool, after
    // filling in the provided name and age fields. Returns NULL if
    // the student pool is exhausted.
    Student* newStudent(const char* name, int age)
    {
        Student* student = NULL;
        if (firstFree < MAX_NUM_STUDENTS) {
            student = &studentPool[firstFree];
            firstFree += 1;
            strncpy(student->name, name, MAX_NAME_SIZE);
            student->name[MAX_NAME_SIZE - 1] = '\0';  // Make sure it's terminated
            student->age = age;
            student->next = NULL;
        }
        return student;
    }

    // Read a single student from a csv input file with student name in first column,
    // and student age in second.
    // Returns: A pointer to a Student record, or NULL if EOF or an invalid
    // student record is read. Blank lines, or lines in which the name is
    // longer than the provided name buffer, or there is no comma in the line
    // are considered invalid.
    Student* readOneStudent(FILE* file)
    {
        char buffer[MAX_LINE_LENGTH];  // Buffer into which we read a line from stdin
        Student* student = NULL;       // Pointer to a student record from the pool

        // Read a line, extract name and age

        char* inputLine = fgets(buffer, MAX_LINE_LENGTH, file);
        if (inputLine != NULL) {        // Proceed only if we read something
            char* commaPos = strchr(buffer, ',');
            if (commaPos != NULL) {
                int age = atoi(commaPos + 1);
                *commaPos = '\0';  // null-terminate the name
                student = newStudent(buffer, age);
            }
        }
        return student;
    }


    Student* readStudents(FILE *file)
    {
        Student* first = NULL;     // Pointer to the first student in the list
        Student* last = NULL;      // Pointer to the last student in the list
        Student* student = readOneStudent(file);
        while (student != NULL) {
            if (first == NULL) {
                first = last = student;   // Empty list case
            } else {
                student -> next = first;
                first = student;
            }
            student = readOneStudent(file);
        }
        return first;
    }

    // printOneStudent: prints a single student, passed by value
    void printOneStudent(Student student)
    {
        printf("%s (%d)\n", student.name, student.age);
    }


    // printStudents: print all students in a list of students, passed
    // by reference
    void printStudents(const Student* student)
    {
        while (student != NULL) {
            printOneStudent(*student);
            student = student->next;
        }
    }



    int main(void)
    {
        FILE* inputFile = stdin;
        if (inputFile == NULL) {
            fprintf(stderr, "File not found\n");
        } else {
            Student* studentList = readStudents(inputFile);
            printStudents(studentList);


        }
    }

1 Answer 1

1

Below code should work. Note that

strchr() 
This returns a pointer to the first occurrence of the character c in the string str, or NULL if the character is not found.

I think you were trying to get age from name string.

#include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>

    #define MAX_LINE_LENGTH 80      // The longest line this program will accept
    #define MAX_NUM_STUDENTS 500    // The maximum number of students this program can handle
    #define MAX_NAME_SIZE 50        // The maximum allowable name length

    typedef struct student_s Student;

    struct student_s {
        char name[MAX_NAME_SIZE];
        int age;
        Student* next;              // Pointer to next student in a list
    };

    // Create a pool of student records to be allocated on demand

    Student studentPool[MAX_NUM_STUDENTS];  // The student pool
    int firstFree = 0;

    // Return a pointer to a new student record from the pool, after
    // filling in the provided name and age fields. Returns NULL if
    // the student pool is exhausted.
    Student* newStudent(const char* name, int age)
    {
        Student* student = NULL;
        if (firstFree < MAX_NUM_STUDENTS) {
            student = &studentPool[firstFree];
            firstFree += 1;
            strncpy(student->name, name, MAX_NAME_SIZE);
            student->name[MAX_NAME_SIZE - 1] = '\0';  // Make sure it's terminated
            student->age = age;
            student->next = NULL;
        }
        return student;
    }

    // Read a single student from a csv input file with student name in first column,
    // and student age in second.
    // Returns: A pointer to a Student record, or NULL if EOF or an invalid
    // student record is read. Blank lines, or lines in which the name is
    // longer than the provided name buffer, or there is no comma in the line
    // are considered invalid.
    Student* readOneStudent(FILE* file)
    {
        char buffer[MAX_LINE_LENGTH];  // Buffer into which we read a line from stdin
        Student* student = NULL;       // Pointer to a student record from the pool

        // Read a line, extract name and age

        char* inputLine = fgets(buffer, MAX_LINE_LENGTH, file);
        if (inputLine != NULL) {        // Proceed only if we read something
            char* commaPos = strchr(buffer, ',');
            if (commaPos != NULL) {
               // int age = atoi(commaPos + 1);
                //printf("age and commaPos is %d,%s \n ",age,commaPos);

                char* name = commaPos+1;
                name[strcspn(name, "\n")] = 0; //remove /n from fgets
                *commaPos = '\0';  // null-terminate the age
                int age = atoi(buffer);
                //printf("age and commaPos is %d,%s \n ",age,name);

                //student = newStudent(buffer, age);
                student = newStudent(name, age);
            }
        }
        return student;
    }


    Student* readStudents(FILE *file)
    {
        Student* first = NULL;     // Pointer to the first student in the list
        Student* last = NULL;      // Pointer to the last student in the list
        Student* student = readOneStudent(file);
        while (student != NULL) {
            if (first == NULL) {
                first = last = student;   // Empty list case
            } else {
                student -> next = first;
                first = student;
            }
            student = readOneStudent(file);
        }
        return first;
    }

    // printOneStudent: prints a single student, passed by value
    void printOneStudent(Student student)
    {
        printf("%s (%d)\n", student.name, student.age);
    }


    // printStudents: print all students in a list of students, passed
    // by reference
    void printStudents(const Student* student)
    {
        while (student != NULL) {
            printOneStudent(*student);
            student = student->next;
        }
    }



    int main(void)
    {
        FILE* inputFile = stdin;
        if (inputFile == NULL) {
            fprintf(stderr, "File not found\n");
        } else {
            Student* studentList = readStudents(inputFile);
            printStudents(studentList);


        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.