Code:
#include<stdio.h>
#include<string.h>
struct Date
{
int Day;
int Month;
int Year;
};
struct Student
{
char Name[30][50];
float grades;
int nbstudent,Max;
struct Date Birthdate;
};
void DisplayTitle();
void DisplayClass(struct Student stx);
void DisplayOldest(struct Student stxxx);
struct Student ReadNBStudent()
{
struct Student Temp;
do
{
printf("Enter the number of students ");
scanf("%i",&Temp.nbstudent);
}
while(Temp.nbstudent>30 || Temp.nbstudent<2);
return Temp;
};
struct Student ReadAllStudent()
{
int I=0;
struct Student Temporary2;
fflush(stdin);
printf("Student \n");
printf("Name ");
gets(Temporary2.Name[I]);
printf("Enter birth date: ");
scanf("%i/%i/%i",&Temporary2.Birthdate.Day,&Temporary2.Birthdate.Month,&Temporary2.Birthdate.Year);
do {
printf("Grade ");
scanf("%f",&Temporary2.grades);
}
while(Temporary2.grades>100 || Temporary2.grades<0);
return Temporary2;
};
struct Student OldestStudent()
{
struct Student Temporary4ST;
if(2010-Temporary4ST.Birthdate.Year>Temporary4ST.Max)
{
Temporary4ST.Max=2010-Temporary4ST.Birthdate.Year;
}
return Temporary4ST;
};
void main()
{
struct Student stclass[50];
struct Student arrayz[20];
int I=0;
DisplayTitle();
//arrayz[I]=ReadNBStudent();
ReadNBStudent();
for(I=0;I<2;I++)
{
stclass[I]=ReadAllStudent();
}
printf("The Class\n ");
for(I=0;I<2;I++)
{
DisplayClass(stclass[I]);
DisplayOldest(stclass[I]);
}
}
void DisplayTitle()
{
printf("\tCollege Lasalle\n");
printf("\t---------------\n");
}
void DisplayClass(struct Student stx)
{
printf("%s\t %i/%i/%i\t %.2f\n",stx.Name,stx.Birthdate.Day,stx.Birthdate.Month,stx.Birthdate.Year,stx.grades);
}
void DisplayOldest(struct Student stxxx)
{
printf("The oldest student is %i years old",stxxx.Max);
}
User enters the number of students between 2 and 30. The program asks for the name, grades, birthdate, then displays the class and the oldest and youngest student.
I have difficulty when displaying the class. Inside the main, in the for loop (I=0;I<2;I++), the 2 must be the users input. I have tried putting ReadNBStudent() equal to an array and then putting the array/size of the array instead of the 2, but it will not work.
And when I want to display the oldest student it tells me that the variable Temporary4ST is uninitialized. Although I'm not 100% sure of what I programmed in OldestStudent() is entirely correct.