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; }
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.
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.
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.Code:struct foo { int x; }; struct bar { struct foo afoo; }; struct baz { struct bar abar; } abaz; scanf( "%d", &abaz.abar.afoo.x );
Quzah.
Hope is the first step on the road to disappointment.
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 ===|
__________________________________________________ _______
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 --is because the typedef is making an alias for a struct with no name, so you just use whatchumacallit everywhere.Code:typedef struct { /* members */ } whatchumacallit;
It's quite confusing and damned unnecessary.
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?
> 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.
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.
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.
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;
How about if I use the <strsafe.h> library? After I use can I prevent buffer overruns?