-2

I'm building a program that uses a struct to record student records. A task is to create a function (findOldest) to compare each record and find the oldest student.

This is my code so far:

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

typedef struct {
    char name[20];
    char surname[20];
    char UUN[6];
    char department[4];
    char gender;
    int age;
} student;

void printStudent(student s) {
    printf("Name: %s %s\n", s.name, s.surname);
    printf("UUN: %s\n", s.UUN);
    printf("Department: %s\n", s.department);
    printf("Gender: %c\n", s.gender);
    printf("Age: %d\n", s.age);
}

student findOldest ( student *studentarr, int len ) {
    int age = 0;
    int i;

    for ( i = 0; i < len; i++ ) {
        if ( age <= studentarr[i].age ) {
            age = studentarr[i].age;
        }
    }
}

int main(void) {

    int i, length;

    student studentarr[6] = {
        { "John", "Bishop", "s1234", "Inf", 'm', 18 },
        { "Lady", "Cook", "s2345", "Eng", 'f', 21 },
        { "James", "Jackson", "s3456", "Eng", 'm', 17 },
    };

    for ( i = 3; i < 6; i++ ) {
        printf("Enter student %d details:\n", i+1);
        printf("Forename: ");
        scanf("%s", studentarr[i].name);

        printf("Surname: ");
        scanf("%s", studentarr[i].surname);

        printf("UUN: ");
        scanf("%s", studentarr[i].UUN);

        printf("Department: ");
        scanf("%s", studentarr[i].department);

        printf("Gender: ");
        scanf(" %c", &studentarr[i].gender);

        printf("Age: ");
        scanf("%d", &studentarr[i].age);

        printf("\n\n");
    }

    for ( i = 0; i < 6; i++ ) {
        printf("Student %d\n", i+1);
        printStudent(studentarr[i]);
        printf("\n\n");
    }

    length = sizeof(studentarr) / sizeof(studentarr[0]);
    printf("Size of studentarr is %d.\n", length);

    return 0;

}

The task was for the program to define the first three elements of studentarr, as done, and have the user define the last three, again as done. Then call the function findOldest. My problem is creating findOldest, I've made an attempt but I'm fairly sure it's wrong.

Trying to compile as is gives the warning;

studentDB2.c:30:1: warning: control reaches end of non-void function
      [-Wreturn-type]
}
^
1 warning generated.

Edit: Okay the function has been changed too;

student findOldest ( student *studentarr, int len ) {
    int age = 0, oldest_index = 0;
    int i;

    for ( i = 0; i < len; i++ ) {
        if ( age <= studentarr[i].age ) {
            age = studentarr[i].age;
            oldest_index = i;
        }
    }
    return studentarr[oldest_index];
}

How do I use a printf statement at the end of main to say, the oldest student is (Student 4, for example) and is 19 (again example) years old?

Edit: I've tried this

printf("The oldest student is %d years old.\n", findOldest(&studentarr[6], length).age);

It's calling the function, and should return studentarr[1], as this is the index with the highest age (21 years). Instead the compiler is returning;

The oldest student is 1297237332 years old.

What is the problem?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 3
    The warning is pretty clear, hint: what does `findOldest` return? – Yu Hao Aug 15 '15 at 11:36
  • 1
    your function is meant to return something, but it doesn't – Giorgi Moniava Aug 15 '15 at 11:37
  • 1
    possible duplicate of [What does "control reaches end of non-void function" mean?](http://stackoverflow.com/questions/6171500/what-does-control-reaches-end-of-non-void-function-mean) – 200_success Aug 15 '15 at 11:45
  • Sorry for similar question but I had other things to add outside of that anyway, which I received help for. – Stephen McGarry Aug 15 '15 at 11:48
  • `printf("The oldest student is %d years old.\n", findOldest(studentarr, length).age);` is the correct way to do it. And `age` in `findOldest` is unnecessary as it isn't used anywhere. – Spikatrix Aug 15 '15 at 13:09
  • It was as easy as that! Thanks so much! – Stephen McGarry Aug 15 '15 at 13:14
  • Maybe you could help again. Rather than having the printf at the end of pain, I've tried ding this instead - printf("The oldest student is:\n"); printstudent(findOldest(studentarr, length)); - but the compiler is saying - warning: implicit declaration of function 'printstudent' is invalid in C99 [ Wimplicit-function-declaration] printstudent(findOldest(studentarr, length)); 1 warning generated. - What would this mean? – Stephen McGarry Aug 15 '15 at 14:21
  • I should add, with this warning, the program won't run. – Stephen McGarry Aug 15 '15 at 14:23
  • Use `printStudent(findOldest(studentarr, length));` instead of `printstudent(findOldest(studentarr, length));`. And to notify a user, use '@' along with their username like `@CoolGuy` it you want to notify/ping me. See [How do comment @replies work?](http://meta.stackexchange.com/questions/43019/how-do-comment-replies-work) for more information on this. – Spikatrix Aug 15 '15 at 14:26
  • @CoolGuy thanks!! It was just the capital letter, something so small.... I'm very grateful for your help :) Also very new to this site so didn't know you could use @. Thanks :) – Stephen McGarry Aug 15 '15 at 14:47
  • You're welcome! :-- D Feel free to ping me if you face issues. – Spikatrix Aug 16 '15 at 05:11
  • @CoolGuy I want to post a new question but unfortunately can't. I'll ask here. The user must input the date in the format dd/mm (so 05/11 for example), I'm using scanf("%d", &day); scanf("%d", &month); but when the user enters, 5/11, the / is picked up and messes with the value for month. How do I make scanf ignore the / character. – Stephen McGarry Aug 16 '15 at 12:40
  • @StephenMcGarry Maybe you want: `if( scanf("%d/%d", &day, &month) != 2) { printf("Invalid input"); int c; while(( c = getchar() ) != '\n' && c != EOF);`. Here, the `scanf` grabs input in the format `/`. The `if` statement ensures that the `scanf` is successful. The loop in the body of the `if` gets rid of the rest of the input from the standard input stream. – Spikatrix Aug 16 '15 at 12:46
  • Ahhhhh, all I had to do was change the scanf to scanf("%d/%d", &day, &month); easy as that, as ever.... Thanks! – Stephen McGarry Aug 16 '15 at 12:50
  • @StephenMcGarry , Yes. That would work. But note that when the user enters an invalid input, your program will go berserk. The code in my earlier comment takes care of this. – Spikatrix Aug 16 '15 at 12:52
  • @CoolGuy Yeah I appreciate that. I'm just doing past paper questions which say you can ignore any erroneous input and assume the user will enter appropriate days and months, basically assume if the user enters February, a day higher than 28 won't be entered etc. But I do appreciate the guidance! The more you know :) – Stephen McGarry Aug 16 '15 at 12:59
  • @StephenMcGarry Ok. Good. And for formatting as code in comments, surround it with backticks(\`) for ex: `\`code\`` – Spikatrix Aug 16 '15 at 13:02
  • @CoolGuy is there a way to send you code over this? Like in a message? I have another question but it's going to hard to understand if I just post it as a comment. – Stephen McGarry Aug 16 '15 at 14:49
  • @StephenMcGarry I'm going to be offline. Post your code quickly in the [C chatroom](http://chat.stackoverflow.com/rooms/54304/c). Or simply give a link via [PasteBin](http://pastebin.com/) – Spikatrix Aug 16 '15 at 14:52
  • @CoolGuy can't, reputation isn't high enough. Any idea how long you'll be away? – Stephen McGarry Aug 16 '15 at 14:53
  • @CoolGuy did PasteBin, here http://pastebin.com/uREt0yUc – Stephen McGarry Aug 16 '15 at 14:55
  • @StephenMcGarry Oh. Curious: How much reputation do you need? And what's the problem with the code? – Spikatrix Aug 16 '15 at 14:56
  • @CoolGuy The compiler is working for array c, but not b, returning this; `After maxToFront, b is 1641904770, 6, 2, 4, 44, 5, -10, -6, 5, 8. After maxToFront, c is 44, 2, 3, -6, 4, 8, -2, 44, 9, 6, 1, 3, 4, -11, 0. Abort trap: 6` why is c working but not b? and what is abort trap 6? – Stephen McGarry Aug 16 '15 at 14:56
  • @CoolGuy Nevermind I just realised it ins't even working properly for array c either. You need 20 reputation – Stephen McGarry Aug 16 '15 at 14:57
  • Think what happens `if ( a[i+1] > a[max_index] )` when `i` is `n-1`. Also, I'm getting a warning that here: `a[0] = temp;`, `temp` might be uninitialized. BTW, The errors happens because you access an invalid memory location. – Spikatrix Aug 16 '15 at 14:58
  • @CoolGuy I see, but I'm struggling to fix it. If I change it to `if ( a[i] > a[max_index] )` Still not working, I know it's not correct but what do I do!? – Stephen McGarry Aug 16 '15 at 15:02
  • @StephenMcGarry I'm going offline. Bye. [FIXED CODE](http://rextester.com/YPFY92066) – Spikatrix Aug 16 '15 at 15:03
  • @CoolGuy Got it! Thanks for the code, but managed to get it working :) – Stephen McGarry Aug 16 '15 at 15:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87195/discussion-between-stephen-mcgarry-and-cool-guy). – Stephen McGarry Aug 17 '15 at 19:09

2 Answers2

5

In your findOldest function, you correctly update the maximum age encountered so far. However, you must also keep track of the index in studentarr at which you found your oldest student so far. Then you must return this student. Try this:

student findOldest ( student *studentarr, int len ) 
{
    int age = 0;
    int i, oldest_index = 0;

    for ( i = 0; i < len; i++ ) {
        if ( age <= studentarr[i].age ) {
            age = studentarr[i].age;
            oldest_index = i;
        }
    }
    return studentarr[oldest_index]; 
}
VHarisop
  • 2,816
  • 1
  • 14
  • 28
2

The function signature is

student findOldest ( student *studentarr, int len )

That is, the function promises to return a student struct. But it doesn't return anything. Fix that by writing a return statement that returns a student.

200_success
  • 7,286
  • 1
  • 43
  • 74
  • Okay I understand what you're saying, but how would I modify my function to save the address of the oldest student? Like it will save the age, but how will it know which student that age relates too? Also how do I write the return statement? return student[i]? Where student[i] is the oldest student? – Stephen McGarry Aug 15 '15 at 11:41
  • Never somebody has just answered exactly what I've asked! Thanks for the pointers :) – Stephen McGarry Aug 15 '15 at 11:49