1
#include <stdio.h>

#include <string.h>

#include <stdlib.h>

typedef struct Student {
    char name[15];
    char lastname[50];
    int age;
} student;
int findYoungest(struct Student students[10], int length) {
    int currentYoungestIndex = 0;
    for (int i = 0; i < length; i++) {
        if (students[currentYoungestIndex].age > students[i].age) {
            currentYoungestIndex = i;
        }
    }
    return currentYoungestIndex;
}

int findOldest(struct Student students[10], int length) {
    int currentOldestIndex = 0;
    for (int i = 1; i < length; i++) {
        if (students[currentOldestIndex].age < students[i].age) {
            currentOldestIndex = i;
        }
    }
    return currentOldestIndex;
}

void printStudentInformation(struct Student students[10], int countOfStudents) {
    if(countOfStudents <= 0) {
        printf("No students were given\n");
    }

    printf("Count: %i\n", countOfStudents);
    for (int i = 0; i < countOfStudents; ++i) {
        printf("Name = %s%s, Age = %d\n", students[i].name, students[i].lastname, students[i].age);
    }

    printf("Youngest: %s%s\n",
           students[findYoungest(students, countOfStudents)].name,
           students[findYoungest(students, countOfStudents)].lastname);
    printf("Oldest: %s%s\n",
           students[findOldest(students, countOfStudents)].name,
           students[findOldest(students, countOfStudents)].lastname);
}


int main() {
    char name[50];
    char lastname[50];
    int age;
    char stop[] = "stop";
    int wroteStop = 1;
    int countOfStudents = 0;
    student * students = NULL;

    while (wroteStop) {
        scanf("%s", name);
        fgets(lastname, 49, stdin);
        name[strcspn(name, "\n")] = 0;
        lastname[strcspn(lastname, "\n")] = 0;

        if (strcmp(name, stop) != 0) {
            scanf("%i", &age);

            // Create dynamic array
            if (!students) {
                students = (student *) malloc(sizeof(student *));
            } else {
                students = (student *) realloc(students, sizeof(countOfStudents + 1));
            }
            // Add student to list.
            strcpy(students[countOfStudents].name, name);
            strcpy(students[countOfStudents].lastname, lastname);
            students[countOfStudents].age = age;
            countOfStudents++;
        } else {
            wroteStop = 0;
        }

    }
    // Print student information
    printStudentInformation(students, countOfStudents - 1);

    free(students);
    students = NULL;
    return 0;

}

Example input:

Erin Smith
10
Matthew Berg
90
Alexandria Garcia
60
Michelle Lewis
85
Amanda Best
34
Michelle Cook
47
Todd Vazquez
13
Erica Moss
50
Sarah Holder
41
Charles Vincent Jr.
29
Joshua Rivera
25
Katherine Peters
56
Natasha Lee
32
Emily Carter
79
Bryan Ingram
87
Gavin Lewis
53
Richard Ellis
51
Nicole Payne
55
Brian Cantrell
20
William Jackson
16
stop

Timed out while waiting for your program to exit. The program might be waiting on input or have an endless loop."

The user types in: Firstname, Surname and Age.

This is appended to a list, which is later iterated through so that we get very student in the list and the youngest/oldest student in the list. If the user types "stop", the output-function named printStudentInformation() is executed

001
  • 13,291
  • 5
  • 35
  • 66
  • There must be a condition on when to exit the program in the question instead of "stop" as input by the user. – Linux Geek Oct 26 '21 at 17:55
  • 3
    First, the `malloc(sizeof(student *))` only allocates the size of a pointer, it should be `malloc(sizeof *students)`. Second, the `sizeof(countOfStudents + 1)` is only the size of an `int`, and should be `(sizeof *students) * (countOfStudents + 1)` – Weather Vane Oct 26 '21 at 17:57
  • 1
    Debug by printing the value of `strcmp(name, stop)`. Is it what you expect when you type in "stop"? – jarmod Oct 26 '21 at 17:58
  • 1
    `scanf("%i", &age);` Probably leaving a newline in `stdin`. – 001 Oct 26 '21 at 18:03
  • It is generally bad to mix the input functions: Please see [fgets() doesn't work after scanf](https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf). Might be OK if the name is all on one line, and will deal with the "Charles Vincent Jr." input, which using `scanf` with `%s` twice, won't. – Weather Vane Oct 26 '21 at 18:06
  • After making the corrections to the memory allocations, a test program using that input did work. – Weather Vane Oct 26 '21 at 18:15
  • The corrections did solve the problem. Thank you! :) @WeatherVane – Suleyman Selcuk Oct 26 '21 at 18:31

0 Answers0