Search code examples
cstructure

I want to write a program to calculate the result of a student by using the structure and print the position


In this program i want to take students information using structure and print the position. And in the quiz section only the best one will count out of three. Now when i run the program it doesn't take any inputs. It just asks for the number of the students and after that it terminates and doesn't take any other inputs. Can someone find out where i am having the problem.

struct student_profile
{
    char name[100];
    int ID[100];
    int Final;
    int mid;
    int attendance;
    int assignment;
    int quiz1;
    int quiz2;
    int quiz3;
} s[100];


int comparison(int quiz1, int quiz2, int quiz3, int quiz_best)
{
    if (quiz1>quiz2 && quiz1>quiz3)
    {
        printf("Best quiz mark is: \n",quiz1);
        quiz_best = quiz1;
    }
    else if (quiz2>quiz1 && quiz2>quiz3)
    {
        printf("Best quiz mark is: \n",quiz2);
        quiz_best = quiz2;
    }
    else
        printf("Best quiz mark is: \n",quiz3);
        quiz_best = quiz3;

    return quiz_best;
}


int main()
{
    struct student_profile;
    int i,n,quiz_best;
    printf("Number of the student: \n");
    scanf("%d",&n);
    for(i=0,i<n; i++;)
    {
        printf("\nStudent No: %d\n",i);
        printf("Name: ");
        scanf("%d", &s[i].name);
        printf("ID: ");
        scanf("%d",&s[i].ID);
        printf("Final: ");
        scanf("%d",&s[i].Final);
        printf("mid: ");
        scanf("%d",&s[i].mid);
        printf("attendance: ");
        scanf("%d",&s[i].attendance);
        printf("assignment: ");
        scanf("%d",&s[i].assignment);
        printf("quiz1: ");
        scanf("%d",&s[i].quiz1);
        printf("quiz2: ");
        scanf("%d",&s[i].quiz2);
        printf("quiz3: ");
        scanf("%d",&s[i].quiz3);

        printf("\n Students Informations \n");

        for(i=0,i<n; i++;)
        {
            printf("\nStudent No: %d\n",i);
            printf("\nName: %s\n",s[i].name);
            printf("\nID: %d\n",s[i].ID);
            printf("\nFinal: %d\n",s[i].Final);
            printf("\nmid: %d\n",s[i].mid);
            printf("\nattendance: %d\n",s[i].attendance);
            printf("\nassignment: %d\n",s[i].assignment);
            printf("\nquiz1: %d\n",s[i].quiz1);
            printf("\nquiz2: %d\n",s[i].quiz2);
            printf("\nquiz3: %d\n",s[i].quiz3);
            printf("\nThe best quiz mark is: ",quiz_best);
            comparison(s[i].quiz1,s[i].quiz2,s[i].quiz3,quiz_best);
        }

    }
} ```

Solution

  • There are many problems in your code:

    • There are missing braces in:

      else
          printf("Best quiz mark is: \n", quiz3);
          quiz_best = quiz3;
      

      It should be:

      else {
          printf("Best quiz mark is: \n", quiz3);
          quiz_best = quiz3;
      }
      

    The declaration struct student_profile; in the main() function is useless.

    scanf() uses %d instead of %s for string values.

    • You have nested loops with the same index variable: for(i=0,i<n; i++;) which are incorrect anyway: it should be for (i = 0; i < n; i++)

    • printf is missing a %d to print the quiz_best variable.

    • quiz_best is printed before it is computed

    • The comparison function should return quiz_best, updating a function argument has no effect on the callers' variable.

    Here is a modified version:

    #include <stdio.h>
    
    struct student_profile {
        char name[100];
        int ID[100];
        int Final;
        int mid;
        int attendance;
        int assignment;
        int quiz1;
        int quiz2;
        int quiz3;
    } s[100];
    
    int comparison(int quiz1, int quiz2, int quiz3) {
        int quiz_best;
    
        if (quiz1 > quiz2 && quiz1 > quiz3) {
            printf("Best quiz mark is: \n", quiz1);
            quiz_best = quiz1;
        } else
        if (quiz2 > quiz1 && quiz2 > quiz3) {
            printf("Best quiz mark is: \n", quiz2);
            quiz_best = quiz2;
        } else {
            printf("Best quiz mark is: \n", quiz3);
            quiz_best = quiz3;
        }
        return quiz_best;
    }
    
    int main() {
        int i, n, quiz_best;
    
        printf("Number of students: \n");
        scanf("%d", &n);
        for (i = 0; i < n; i++) {
            printf("\nStudent No: %d\n", i);
            printf("Name: ");
            scanf("%99s", s[i].name);
            printf("ID: ");
            scanf("%99s", s[i].ID);
            printf("Final: ");
            scanf("%d", &s[i].Final);
            printf("mid: ");
            scanf("%d", &s[i].mid);
            printf("attendance: ");
            scanf("%d", &s[i].attendance);
            printf("assignment: ");
            scanf("%d", &s[i].assignment);
            printf("quiz1: ");
            scanf("%d", &s[i].quiz1);
            printf("quiz2: ");
            scanf("%d", &s[i].quiz2);
            printf("quiz3: ");
            scanf("%d", &s[i].quiz3);
        }
        printf("\n Students Informations:\n");
    
        for (i = 0; i < n; i++) {
            printf("Student No: %d\n", i);
            printf("Name: %s\n", s[i].name);
            printf("ID: %d\n", s[i].ID);
            printf("Final: %d\n", s[i].Final);
            printf("mid: %d\n", s[i].mid);
            printf("attendance: %d\n", s[i].attendance);
            printf("assignment: %d\n", s[i].assignment);
            printf("quiz1: %d\n", s[i].quiz1);
            printf("quiz2: %d\n", s[i].quiz2);
            printf("quiz3: %d\n", s[i].quiz3);
            quiz_best = comparison(s[i].quiz1, s[i].quiz2, s[i].quiz3);
            printf("The best quiz mark is: %d\n", quiz_best);
            printf("\n");
        }
        return 0;
    }