Thread: Nested Structure help ~

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Siaw View Post
    Ok...below is the code. Am I do right for (iv)?

    Code:
    int main(void)
    
        {
            COURSE IBcourse;
    Yep.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Thanks for you again. But can you teach or give me some hints me how to done the below question by using the following codes please. (Because I am not good in nested structure)

    (v) Write a C program segment that prompts the user to input 10 sets of student records. Store the data entered into the members StudentData in the variable IBcourse

    My coding so far...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
        struct DATE {
    
            int month;
    
            int day;
    
            int year;
    
    } DATE;
    
        typedef struct{
    
            char name[30];
    
            char ic[30];
    
            struct DATE birthdate;
    
      } STUDENT;
    
      typedef struct {
    
            char course[30];
    
            struct DATE commencingdate; // struct need struct
    
            STUDENT studentdata; // typedef no need struct
    
      } COURSE;
    
    
      int main(void)
    
        {
            COURSE IBcourse;
    
            int i;
    
            for(i=0; i<10; i++) {
    
                printf("Please input %d students data: ", i);
                gets(COURSE.IBcourse);
    
    
            }
    
    
    
    
            return 0;
    
        }

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Have you learned about arrays? If you are supposed to store 10 things in 10 different places, then you probably want IBcourse to be an array of 10 courses. Next, you want to use the correct method to read the input you want. Each COURSE has a bunch of different points of data that you need to fill, so for each one, you need to fill each point:

    The name of the course.
    The date (according to each field you have for your date structure).
    The student (according to each point in the student structure).

    You need to fill each piece of each structure you have. Consider using scanf, which lets you input specific formats, or fgets to read a whole line, and then sscanf to parse that line for what you expect.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Can you please get me an example how to start reading user input? I know how use use for loop but I don't know how to get start with store input into the IBcourse course.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct foo { int x; };
    struct bar { struct foo afoo; };
    struct baz { struct bar abar; } abaz;
    
    scanf( "%d", &abaz.abar.afoo.x );
    Something like that. You need to reference each sub piece with the appropriate dot operator. Here, I have a structure baz, and an instance of it as abaz. Inside abaz is abar, and inside that is an afoo, inside afoo is an integer.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    My code below...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
        struct DATE {
    
            int month;
    
            int day;
    
            int year;
    
    } DATE;
    
        typedef struct{
    
            char name[30];
    
            char ic[30];
    
            struct DATE birthdate;
    
      } STUDENT;
    
      typedef struct {
    
            char courseTitle[124];
    
            struct DATE commencingdate; // struct need struct
    
            STUDENT studentdata[10]; // typedef no need struct
    
      } COURSE;
    
    
      int main(void)
    
        {
            COURSE IBcourse;
    
            int i;
    
            printf("IBCourse Title: ");
            fgets(IBcourse.courseTitle, sizeof(IBcourse.courseTitle),stdin);
    
            printf("\nStart Date (month day year): ");
            scanf("%d %d %d", &IBcourse.commencingdate.month, &IBcourse.commencingdate.day, &IBCourse.commencingdate.year);
    
    
    
    
            return 0;
    
        }
    __________________________________________________ _____

    How can I solve below error?

    C:\Documents and Settings\Administrator\Desktop\Q5.c||In function 'main':|
    C:\Documents and Settings\Administrator\Desktop\Q5.c|46|error: 'IBCourse' undeclared (first use in this function)|
    C:\Documents and Settings\Administrator\Desktop\Q5.c|46|error: (Each undeclared identifier is reported only once|
    C:\Documents and Settings\Administrator\Desktop\Q5.c|46|error: for each function it appears in.)|
    ||=== Build finished: 3 errors, 0 warnings ===|
    __________________________________________________ _______

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You either want to call the thing struct DATE or just DATE. You can't have it both ways.

    One of the reasons I hate this syntax --
    Code:
    typedef struct {
    /* members */
    } whatchumacallit;
    is because the typedef is making an alias for a struct with no name, so you just use whatchumacallit everywhere.

    It's quite confusing and damned unnecessary.

  8. #23
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    I also hope that I can just call the DATE struc but because of below requirement....
    (v) Write a C program segment that prompts the user to input 10 sets of student records. Store the data entered into the members StudentData in the variable IBcourse.
    I must store them in the variable IBcourse, is that really no others way besides call directly?

  9. #24
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > How can I solve below error?
    > C:\Documents and Settings\Administrator\Desktop\Q5.c||In function 'main':|
    > C:\Documents and Settings\Administrator\Desktop\Q5.c|46|error: 'IBCourse' undeclared (first use in this function)|
    Well you could check the spelling, and note that everywhere else, "Course" has a lower case 'c' at the start.

    Then you need a slap for doing program development (and no doubt surfing the web) when logged in as Administrator.
    Or you could just wait until the first program you write which attempts to modify files goes so badly wrong that it trashes your entire system.

    Oh, and delete DATE from line 12, as this just creates a global variable called DATE, with a type of "struct DATE"
    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. #25
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Siaw View Post
    How can I solve below error?

    C:\Documents and Settings\Administrator\Desktop\Q5.c||In function 'main':|
    C:\Documents and Settings\Administrator\Desktop\Q5.c|46|error: 'IBCourse' undeclared (first use in this function)|
    C:\Documents and Settings\Administrator\Desktop\Q5.c|46|error: (Each undeclared identifier is reported only once|
    C:\Documents and Settings\Administrator\Desktop\Q5.c|46|error: for each function it appears in.)|
    ||=== Build finished: 3 errors, 0 warnings ===|
    Your compiler is telling you what the problem is....
    Read your code... line 46... pay attention to spelling and capitilization....

  11. #26
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Very thanks you everyone ~ Below is my code so far...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
        struct DATE {
    
            int month;
    
            int day;
    
            int year;
    
       } ;
    
        typedef struct{
    
            char name[30];
    
            char ic[30];
    
            struct DATE birthdate;
    
      } STUDENT;
    
      typedef struct {
    
            char courseTitle[124];
    
            struct DATE commencingdate; // struct need struct
    
            STUDENT studentdata[10]; // typedef no need struct
    
      } COURSE;
    
    
      int main(void)
    
        {
            COURSE IBcourse;
    
            int i;
    
            printf("IBCourse Title: ");
            fgets(IBcourse.courseTitle, sizeof(IBcourse.courseTitle),stdin);
    
            printf("\nStart Date (month day year): ");
            scanf("%d %d %d", &IBcourse.commencingdate.month, &IBcourse.commencingdate.day, &IBcourse.commencingdate.year);
    
            while(getchar() != '\n');
    
            printf("Please enter student data for 10 students!\n");
    
            for (i = 0; i < 10; i++) {
    
                printf("Name: ");
                fgets(IBcourse.studentdata[i].name, sizeof(IBcourse.studentdata[i].name), stdin);
    
                printf("ICNo: ");
                fgets(IBcourse.studentdata[i].ic, sizeof(IBcourse.studentdata[i].ic), stdin);
    
                printf("Birthday (month day year): ");
                scanf("%d %d %d", &IBcourse. studentdata[i]. birthdate.month, &IBcourse. studentdata[i]. birthdate.day, &IBcourse. studentdata[i]. birthdate.year);
    
                while(getchar() != '\n');
    
            }
    
    
            return 0;
    
        }
    __________________________________________________ _____

    I would like to ask can I change all fgets into gets? Any bad effect will happen? Because I hope to make my program more simple.
    Last edited by Siaw; 10-08-2011 at 11:38 PM.

  12. #27
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you substitute scanf() for fgets() you will run into a problem that scanf() when using %s to read strings stops at the first blank it finds...
    You enter Bobby Smith ... you get Bobby ... with Smith and <enter> left in the buffer for the next scanf() to find.

    Each function has it's place...

    But you should know that fgets() stores newlines (\n ... <enter> key) and you should clean them out of your text before you store the structs.

  13. #28
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Did gets stored new line too? Can I change all my fgets into gets?

  14. #29
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Siaw View Post
    Did gets stored new line too? Can I change all my fgets into gets?
    The reason we use fgets() is that we can prevent buffer overruns... someone typing "you gotta be kidding me this can't happen" ... in to a 12 byte input buffer... gets() offers no such protection.

    It's fairly easy to take the newline off...
    Code:
    char str[100];
    
    fgets(str,99,stdin);
    
    if (str[strlen(str) - 1] == '\n')
      str[strlen(str) - 1] = 0;

  15. #30
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    How about if I use the <strsafe.h> library? After I use can I prevent buffer overruns?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing and using a Array nested Structure
    By Jaxtomtom89 in forum C Programming
    Replies: 2
    Last Post: 11-30-2010, 02:50 AM
  2. pointer to nested structure
    By wahid in forum C Programming
    Replies: 2
    Last Post: 11-10-2010, 04:05 AM
  3. Displaying Data from a Nested Structure
    By shazg2000 in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 10:16 AM
  4. initializing nested structure arrays
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 06-10-2004, 10:58 PM
  5. nested structure help
    By whistler in forum C Programming
    Replies: 1
    Last Post: 05-17-2002, 10:48 AM