Thread: Modifying a struct array that uses User Input. Please help?

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    2

    Question Modifying a struct array that uses User Input. Please help?

    Okay I am a relatively new programming student, and brand new to C. I have an assignment to calculate a GPA for an unknown number of students taking an unknown number of classes. I eventually came up with the code below, but while I can easily pull the student number and the students name from the struct, the grades output always comes up as 0. I cannot, for the life of me, figure out why. Can anyone shed some light on this for a dummy like me? lol I would really appreciate any advice you can offer.

    Code:
    #include <stdio.h>
    struct student{
        char name[50];
        int student_number;
        float grades;
    };
    int main(){
        int number_of_students;
        int number_of_courses_taken;
        float remember_grade=0;
        int display_grades_counter=0;
        printf("How many students would you like to enter?");
        scanf("%d", &number_of_students);
        struct student s[number_of_students];
        
        printf("Enter information of students:\n");
        for(int entry_counter=1;entry_counter!=number_of_students;entry_counter++)
        {
            s[entry_counter].student_number=entry_counter+1;
            printf("\nFor Student number %d\n",s[entry_counter].student_number);
            printf("Enter name: ");
            scanf("%s",s[entry_counter].name);
            printf("How many courses did this student take?");
            scanf("%d", &number_of_courses_taken);
            remember_grade=0;
            for(int grade_counter=0; grade_counter<number_of_courses_taken;grade_counter++)
            {
                printf("Enter grade: ");
                scanf("%d", &s[entry_counter].grades);
                printf("%d", s[entry_counter].grades);
                remember_grade=remember_grade+s[entry_counter].grades;
                if(grade_counter==number_of_courses_taken-1)
                {
                    //
                    printf("%d", remember_grade);
                    printf("%d", s[entry_counter].grades);
                    s[entry_counter].grades=remember_grade/number_of_courses_taken;
                }
                else
                printf("Please Continue!\n");
            }
        }
        printf("Displaying information of students:\n\n");
        
        for (int display_grades_counter=0; display_grades_counter<number_of_students; display_grades_counter++)
        {
         
         printf("Name: ");
         printf("%s", s[display_grades_counter].name);
         printf("GPA: %d",s[display_grades_counter].grades);
       }
       return 0;
    }

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    grades is of type float and yet you are using %d in scanf() and printf(). You should be using %f.

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    2

    Thank you

    Quote Originally Posted by Hodor View Post
    grades is of type float and yet you are using %d in scanf() and printf(). You should be using %f.
    I have no idea how I missed that. While playing with my code it looks like I started changing to float and never finished fixing everything. Thank you so much!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It seems somewhat ironic that arguably the most important variable in the code has the shortest name, whereas simple loop counters have very long names.

    gcc -Wall prog.c would have told you about your printf/scanf mistake.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to create an user input for 2D array
    By Idan Damri in forum C Programming
    Replies: 21
    Last Post: 08-24-2014, 04:28 PM
  2. c struct input from user error
    By shivi_php in forum C Programming
    Replies: 5
    Last Post: 08-25-2013, 09:54 PM
  3. User input for array size.
    By coolmoniker in forum C++ Programming
    Replies: 27
    Last Post: 08-24-2006, 11:34 PM
  4. creating a struct based on user input
    By rodrigorules in forum C Programming
    Replies: 1
    Last Post: 09-15-2005, 06:16 PM
  5. array and user input
    By enlinux in forum C Programming
    Replies: 2
    Last Post: 07-20-2003, 02:08 AM