
Structure, where is one array inside the struct...
Groovy hepcat Jarmo Tiittanen was jivin' on Wed, 16 Jun 1999 16:29:41
+0300 in comp.lang.c.
Structure, where is one array inside the struct...'s a cool scene! Dig
it!
Quote:
>I dont know how to make a structure,
>where is student number (integer), student name (string) and
>student grades included subject (char) and grade (integer).
I'm afraid your grasp of English seems to be quite poor, so this is
hard to understand. But fortunately I could understand from the code
you posted.
Quote:
>struct STUDENT
>{
> char stunum[5];
I thought this was supposed to be an int. What's with the array of
char?
Quote:
> int stuname[30];
And this should be an array of char, not array of int.
Quote:
> char grades[5][10]; // this is my problem
Now, here you're way off base. What you need is an array with 5
elements, but an array of what? Well, each element must be able to
hold two items of data: a letter representing the subject, and a grade
(presumeably from 0 to 100). Sounds like another stuct to me.
Quote:
> };
>struct STUDENT array[100];
>Allowed subjects are O, V, L, S and T and grade from 0 to 5.
Define the struct with an inner nested struct, something like this:
struct STUDENT
{
int num;
char name[30];
struct
{
char subject;
int grade;
}grade_data[5];
Quote:
};
You can then read input into members of the struct something like
this:
#include <stdio.h>
#include <ctype.h>
#define MAX_NUM_STU 100
int yn_prompt(char *prompt);
int get_grade(char subject);
struct STUDENT
{
int num;
char name[30];
struct
{
char subject;
int grade;
}grade_data[5];
Quote:
};
int main(void)
{
struct STUDENT stu[MAX_NUM_STU];
int i, j;
char subject[5] = "OVLST";
i = 0;
do
{
stu[i].num = i;
printf("\nName of student: ");
fflush(stdout);
fgets(stu[i].name, sizeof stu[i].name, stdin);
for(j = 0; j < 5; j++)
{
stu[i].grade_data[j].grade =
get_grade(stu[i].grade_data[j].subject = subject[j]);
}
i++;
}while(i <= 100 && yn_prompt("\nEnter another?"));
return 0;
Quote:
}
int yn_prompt(char *prompt)
{
char buf[20];
int c;
do
{
printf("%s (Y|N): ", prompt);
fflush(stdout);
fgets(buf, sizeof buf, stdin);
}while((c = tolower(buf[0])) != 'y' && c != 'n');
return c == 'y';
Quote:
}
int get_grade(char subject)
{
char buf[20];
int n;
do
{
printf("Grade for subject %c: ", subject);
fflush(stdout);
fgets(buf, sizeof buf, stdin);
}while(sscanf(buf, "%d", &n) != 1 || n < 0 || n > 100);
return n;
Quote:
}
--
----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----
-------------- Shaggy was here! ---------------
http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============