structure with array elements

This is a discussion on structure with array elements within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to create a program that will print and calculate a students average grade given 20 results. The ...

  1. #1
    l_l
    l_l is offline
    Registered User
    Join Date
    Jul 2012
    Posts
    9

    structure with array elements

    Hi,
    I'm trying to create a program that will print and calculate a students average grade given 20 results. The user would have to enter the grades in manually. I am trying it out with 2 students and 3 results for simplicity. What I had in mind was:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct name 
    {
        char fname[20];
        char lname[20];
    };
    
    struct student
    {
        struct name names;
        float grade[3];
    };
    
    int main (void)
    {
        int i, j, k,l;
        struct student input[2];
            
        for (i = 0; i < 2; i++)
        {
            puts("Enter the students first name:");
            gets (input[i].names.fname);
            puts ("Enter the last name:");
            gets (input[i].names.lname);
            puts("Enter the students three (3) grades:");
            
            for (j = 0; j < 3; j++)
            {
                scanf ("%.2f", &input[i].grade[j]);
                
                while (getchar () != '\n')
                {
                    continue;
                }
            }
            
            putchar ('\n');
        }
        
        for (k = 0; k < 2; k++)
        {
            printf ("Student %d: %s %s, grades: ", k+1, input[k].names.fname, input[k].names.lname);
                
            for (l= 0; l < 3; l++);
            {
                printf ("%.2f, ", input[l].grade[l]);
            }
        }    
        
         return 0;
    }
    The program takes the users input, but fails to print the grades correctly.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,234
    Code:
    for (l= 0; l < 3; l++);  // <--- you have an extra semi-colon here
    {
        printf ("%.2f, ", input[l].grade[l]);  // <--- I imagine you meant:  printf ("%.2f, ", input[k].grade[l]);
    }
    edit:
    - avoid "gets()" - you should use "fgets()" instead
    - avoid using lower-case 'l' as a variable name - it bears a resemblance to '1' (one) or 'I' (capital I) in certain fonts.
    Last edited by Matticus; 09-06-2012 at 11:25 PM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    So what output are you getting, and what would the correct output be?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    look here
    Code:
            printf ("%.2f, ", input[l].grade[l]);
    You'll always print the 1st grade of the 1st student, the 2nd grade of the 2nd student, the 3rd grade of the 3rd student, ..., ...

  5. #5
    l_l
    l_l is offline
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    I have corrected the error and am still unable to get the correct entries to print on screen. I would enter:
    Code:
    a = input[0].names.fname 
    b = input[0].names.lname
    1 = input[0].grade[0]
    2 = input[0].grade[1]
    3 = input[0].grade[2]
    but, would I get in return is:
    Code:
    Student 1: a b, grades: 0.00, Student 2: c d, grades: 0.00

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,234
    Code:
    scanf ("%.2f", &input[i].grade[j]);
    You're scanning for a floating point value containing two decimal places.

    My first suggest - run the program and enter "1.00", "2.00", "3.00" to see if the problem you're seeing goes away.

    My second suggestion - just get rid of the format specifier in the "scan()" and retry it with your original values ("1", "2", "3").

  7. #7
    l_l
    l_l is offline
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    Neither modifications are printing the correct output, when I changed to
    Code:
    scanf ("%f", &input[i].grade[j]);
    it prints:
    Code:
    Student 1: a b, grades: 0.00, Student 2: c d, grades: -486501284064.00
    entering both 1, 2, 3 or 1.00, 2.00, or 3.00

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Code:
            for (l= 0; l < 3; l++);
            {
                printf ("%.2f, ", input[l].grade[l]);
            }
    This loop comes with a costly typo - notice the semicolon on the end of the closing parens. This explains the problem, because l is actually 3 when you get to the printf statement, causing out of bounds access. You are also using scanf() incorrectly, at least according to my manual. Specification for precision is printf only. Since you want to read stuff like "1", "2", "3" anyway, it doesn't make sense to try.

    I made some functions when I tested your I/O, might want to see them:
    Code:
    void GetString(char *string, size_t maxLength)
    {
        if (fgets(string, maxLength, stdin) != NULL) {
            char *eol = strchr(string, '\n');
            if (eol != NULL) {
                *eol = '\0';
            }
        }
    }
    
    float GetGrade(void)
    {
        float grade=0.0f;
        int check = scanf("%f", &grade);
    
        while (check != 1) {
            int c;
            fprintf(stderr, "GetGrade error: reading error\n");
            while ((c = getchar()) != '\n' && c != EOF)
                /*continue*/ ;
    
            printf("Try again:\n");
            check = scanf("%f", &grade);
        }
    
        return grade;
    }
    Optional improvement if you want it.
    Last edited by whiteflags; 09-07-2012 at 07:59 PM. Reason: Correction: field with is possible for scanf.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning structure elements to an array
    By anndruu12 in forum C Programming
    Replies: 14
    Last Post: 12-08-2010, 04:25 PM
  2. MPI_Scatter structure elements
    By Chris_1980 in forum C Programming
    Replies: 0
    Last Post: 06-30-2010, 04:46 AM
  3. giving value to structure elements
    By sarathius in forum C Programming
    Replies: 3
    Last Post: 04-21-2008, 01:24 AM
  4. How to compare structure elements
    By khpuce in forum C Programming
    Replies: 6
    Last Post: 04-10-2005, 11:40 AM
  5. Copy structure elements into byte array
    By irncty99 in forum C Programming
    Replies: 4
    Last Post: 03-15-2005, 12:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21