Thread: Help with C structures

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    14

    Exclamation Help with C structures

    I just learned about c structures the other day but I can't seem to get them to work. It give the wrong result at the end.
    The warnings I'm getting are:
    In function `main':
    24 [Warning] comparison between pointer and integer
    26 [Warning] assignment makes integer from pointer without a cast
    27 [Warning] passing arg 1 of `strcpy' from incompatible pointer type
    29 assignment makes integer from pointer without a cast

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <string.h>
    struct student
    {
           char name [50];
           char id [50];
           char score [50];
    };
    
    int main ()
    {
        int i;
        char sum=0, high=0,avg, highest[50][50];
        struct student stu[i];
        for (i=0; i<=4; i++)
        {
            printf ("Enter the student's name.");
            scanf ("%s", &stu[i].name);
            printf ("Enter the student's ID.");
            scanf ("%s", &stu[i].id);
            printf ("Enter the student's score.");
            scanf ("%f", &stu[i].score);
            if (stu[i].score>high)
            {
               high = stu[i].score;
               strcpy (highest,stu[i].name);
            }
            sum = sum + stu[i].score;
        }
        avg = sum/5;
        printf ("Student %s got the highest grade of %f", highest, high); 
        system ("pause");
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want score to be a floating-point number, then don't declare it as a char array.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Quote Originally Posted by tabstop View Post
    If you want score to be a floating-point number, then don't declare it as a char array.
    i get the same errors when i declare it as a float

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No, the first two warnings will go away. For the last, if you want highest to be a string, then you don't need 50 strings of 50 characters each as you have it.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Quote Originally Posted by tabstop View Post
    No, the first two warnings will go away. For the last, if you want highest to be a string, then you don't need 50 strings of 50 characters each as you have it.
    I changed it to a float and now I'm getting the following errors.

    25 invalid operands to binary >
    27 incompatible types in assignment
    30 invalid operands to binary +


    I'm sorry if I'm just not getting this but I'm new at programming so please bare with me.


    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <string.h>
    struct student
    {
           char name [50];
           char id [50];
           float score [50];
    };
    
    int main ()
    {
        int i;
        float sum=0, high=0,avg;
        char highest[50];
        struct student stu[i];
        for (i=0; i<=4; i++)
        {
            printf ("Enter the student's name.");
            scanf ("%s", &stu[i].name);
            printf ("Enter the student's ID.");
            scanf ("%s", &stu[i].id);
            printf ("Enter the student's score.");
            scanf ("%f", &stu[i].score);
            if (stu[i].score>high)
            {
               high = stu[i].score;
               strcpy (highest,stu[i].name);
            }
            sum = sum + stu[i].score;
        }
        avg = sum/5;
        printf ("Student %s got the highest grade of %f", highest, high); 
        printf ("The average of all the scores are %f", avg);
        system ("pause");
        return 0;
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Does each student have 50 scores?

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Quote Originally Posted by tabstop View Post
    Does each student have 50 scores?
    Each student is suppose to have 1 score but the algorithm we got to work with set the array to 50.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No, you just typed [50] in because you weren't paying attention. Change it to float score; and be done with it.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Quote Originally Posted by tabstop View Post
    No, you just typed [50] in because you weren't paying attention. Change it to float score; and be done with it.
    The algorithm was done by a member of the class and the teacher approved of it so we just assumed it was correct but THANK YOU so much for all the help. It was very much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM