Search code examples
cstructure

I am not getting any output using structures


I'm supposed to print the name of the student with the highest average but my programme doesn't output anything any solutions? When I tried to change %s into %c in printf I got the output successfully but obviously only the first character how do I output the entire string?

#include<stdio.h>
#include "LV2 Z2.h"
int main()
{
    int n,i;
    struct student students[40];
    
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    printf("Enter the name:\n");
    scanf("%s",&students[i].name);
    printf("Enter the surname:\n");
    scanf("%s",&students[i].surname);
    printf("Enter the ID:\n");
    scanf("%d",&students[i].studentID);
    printf("Enter the average:\n");
    scanf("%f",&students[i].average);
    printf("Enter the date:\n");
    scanf("%s",&students[i].date);
        
        
    }
    float max = 0;
    int bestStudent = 0;
    for(i=0;i<n;i++)
    {
     if(students[i].average > max) {
    max = students[i].average;
    bestStudent = i;
    }
    }
    printf("Student : %s %s ima najbolji prosjek",students[bestStudent].name,students[bestStudent].surname);
    return 0;
}



struct student 
{
    char name[100];
    char surname[100];
    int studentID;
    float average;
    char date[100];
    
};

Solution

  • Without having your header file, I ran your program. I had to move your structure definition to be above main so that main recognizes it. Else, it won't compile. Did you previous have the structure defined in your header file, but you just moved it into main for the purposes of showing us? If so, that would cause your program not to work.

    I also changed your scanf()'s involving %s. The name of an array evaluates as the address of the first element i.e.

    int myArray[20]; // myArray evaluates as &myArray[0]
    

    So, remove the & symbol with all of your scanf's involving %s.

    After making these changes, I ran your program and it seemed to work fine.

    MAIN

    #define _CRT_SECURE_NO_WARNINGS
    
    #include<stdio.h>
    
    struct student
    {
        char name[100];
        char surname[100];
        int studentID;
        float average;
        char date[100];
    
    };
    
    int main()
    {
        int n, i;
        struct student students[40];
    
        scanf("%d", &n);
        for (i = 0; i < n; i++)
        {
            printf("Enter the name:\n");
            scanf("%s", students[i].name);
            printf("Enter the surname:\n");
            scanf("%s", students[i].surname);
            printf("Enter the ID:\n");
            scanf("%d", &students[i].studentID);
            printf("Enter the average:\n");
            scanf("%f", &students[i].average);
            printf("Enter the date:\n");
            scanf("%s", students[i].date);
    
    
        }
        float max = 0;
        int bestStudent = 0;
        for (i = 0; i < n; i++)
        {
            if (students[i].average > max) {
                max = students[i].average;
                bestStudent = i;
            }
        }
        printf("Student : %s %s ima najbolji prosjek", students[bestStudent].name, students[bestStudent].surname);
        return 0;
    }
    

    OUTPUT

    4
    Enter the name:
    John
    Enter the surname:
    Smith
    Enter the ID:
    111
    Enter the average:
    90
    Enter the date:
    2/6/21
    Enter the name:
    Sarah
    Enter the surname:
    Jones
    Enter the ID:
    222
    Enter the average:
    95
    Enter the date:
    2/6/21
    Enter the name:
    Mike
    Enter the surname:
    Johnson
    Enter the ID:
    333
    Enter the average:
    80
    Enter the date:
    2/6/21
    Enter the name:
    Bob
    Enter the surname:
    Sampson
    Enter the ID:
    444
    Enter the average:
    70
    Enter the date:
    2/6/21
    Student : Sarah Jones ima najbolji prosjek