#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Help with Structures


    Currently I am working with structures, however I am not quite sure how to use the printf function after the input has been stored. Below is my code. Suggestions? Thanks in advance by the way :)

    Code:
    #include <stdio.h>
    
    typedef struct
    {
         char name[50];
         float grade[4];
         float aver;
         int pos;
    }stud;
    
    stud all[15];
    stud read();
    int a;
    
    int main()
    {
         for(a=0;a<15;a++)
         {
              stud[a]=read();
              printf("HELP HERE PLEASE");
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    Something like
    stud students[15];

    Then
    students[a] = read();
    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
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    You've created a new datatype called stud. You then declared an array, all, whose elements are of type stud.

    This line
    stud[a]=read();
    makes no sense whatsoever. There is no array named "stud". There is, however, an array named "all", so you should be using it instead.

    The syntax for accessing a field in a struct is this: <struct variable>.<fieldname>
    So to access the aver field of the second struct in the array, you would write this: all[1].aver
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    Thanks for the help. I have on final question. I dont want to type in all the data when it comes to doing a test run of my program. Is it possible to read all of the input from a text file?
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    Of course you can read input from a file.

    scanf("%d", &num);
    can be written as
    fscanf(stdin,"%d", &num);

    From there, it's just a small step to
    FILE *fp = fopen("data.txt","r");
    fscanf(fp,"%d", &num);
    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
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,075
    Rep Power
    1802
    Originally Posted by bass123
    I dont want to type in all the data when it comes to doing a test run of my program. Is it possible to read all of the input from a text file?
    If your manual input is via stdin (i.e. you are using standardio functions such as getchar() etc.), then you can use redirection to take input from a file rather than manual input. So for example if you have a file grades.dat containing:

    Joe Bloggs
    14
    Sam Spoon
    23
    Jane Doe
    24
    John Doe
    20



    And a program called grades.exe, you can redirect the file to stdin thus:

    grades <grades.dat

    and the data will be input as if you had typed it from the keyboard.

    This approach means that you can write the code for manual input but test with a file rather than specifically writing it for file access as suggested by Salem. The choice would depend on your requirements.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    structure problem


    This is meaningful way:

    stud all[15];

    all[0].aver=18;
    all[1].aver=28;
    all[2].aver=45;

    all[0].name="Collingam";

IMN logo majestic logo threadwatch logo seochat tools logo