Help with structures

This is a discussion on Help with structures within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to understand how to use structures. I've written this code and it runs but when i try ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    8

    Help with structures

    Hi, I'm trying to understand how to use structures. I've written this code and it runs but when i try to modify the ID of a student in the structure the program crashes, up until that point it runs. Why ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    struct student{
        int id;
        char name[100];
        int age;
    };
    
    void main(){
        struct student a;
    
        a.id=0;
        strcpy(a.name, "Andre");
        a.age=0;
        printf("%i\n%s\n%i\n", a.id, a.name, a.age);
        system("PAUSE");
        printf("enter a new id: ");
        scanf("%i\n", a.id);
        printf("ID: %i\n", a.id);
        system("PAUSE");
    }

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    You have to pass it as a pointer for scanf. Should be

    Code:
    scanf("%i", &a.id);
    Last edited by Syscal; 05-11-2012 at 05:59 PM.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    what a rookie mistake lol

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    OK so now that i have that I also need to be able to load an array of structures from a text file so I may need to spend some time understanding that one.... If anyone knows how i could go about doing this please don't hesitate to lend a helping hand :-D

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Start with a proper main function. Read this: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com.

    Then, to read a whole file, you'll need to know how to open and close it, and you'll need to know how to read out of it. Since it's a text-based data file, you should look into the scanf function. Also, since you're reading a whole file, presumably with more than one entry, you will need a loop.

    I suggest you read some tutorials on file handling and loops in C (we have some on this site, and Google has plenty more). Post back your best effor we'll go from there.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    OK i need to convert what i wrote so far into a system that simply takes input from a text file instead of from console input upon execution.


    HERE's the code, it's very straightforward:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 4 //how many students u will enter
    
    
    struct student{
        char last[100],first[100];
        int t1,t2,t3;
        float avg;
        char grade;
    };
    
    void load(struct student s[], int n){
        int i;
        for(i=0;i<n;i++){
            printf("Enter last name: ");
            gets(s[i].last);
            printf("enter first name: ");
            gets(s[i].first);
            printf("enter 3 scores");
            scanf("%d%d%d",&s[i].t1,&s[i].t2,&s[i].t3);
            //calc avg
    s[i].avg=(s[i].t1+s[i].t2+s[i].t3)/(float)3;
    
    if(s[i].avg>70)
        s[i].grade='p';
    else
        s[i].grade='f';
    fflush(stdin);
        }
    }
    
    void print(struct student s[], int n)
    {
        for(int i=0;i<n;i++)
        {
            printf("first name is %s, last name is %s\n", s[i].first, s[i].last);
            printf("score 1 is %d, score 2 is %d, score 3 is %d\n", s[i].t1, s[i].t2, s[i].t3);
            printf("the average is %f\n", s[i].avg);
            printf("The grade is %c\n", s[i].grade);
        }
    }
    
    void sort(struct student s[], int n)
    {
        int i, j;
        student t;
        for(i=0; i<n-1;i++)
            for(j=0;j<n-1;j++)
            {
                if (s[j].avg<s[j+1].avg)//decending order
                {
                    t=s[j];
                    s[j]=s[j+1];
                    s[j+1]=t;
                }
            }
    }
    
    void passed(struct student s[], int n)
    {
        int count=0;
    
        for(int i=0;i<n;i++)
        {
            if(s[i].grade=='p')
                count++;
            printf("passed count is %d\n", count);
        }
    }
    
    void main()
    {
        student s[SIZE];
        load (s, SIZE);
        sort (s, SIZE);
        print (s, SIZE);
        passed (s, SIZE);
        system("PAUSE");
    }


    Any idea's on how i can take input from a text file instead of console?

  7. #7
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring structures inside structures ?
    By jamaican1231 in forum C Programming
    Replies: 6
    Last Post: 04-13-2010, 03:40 PM
  2. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 12:24 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. Accessing structures contained in structures
    By enigmaatj in forum C Programming
    Replies: 1
    Last Post: 04-18-2002, 08:53 AM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

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