Thread: functions/personalized type/structure issue

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    functions/personalized type/structure issue

    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.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    No one has any idea ?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Why are you using gets() and fflush(stdin)? And void main()??? Are they actually teaching you that crap? If so, you're getting ripped off.

    ReadNBStudent() reads the number of students and then ignores what was entered.
    In C and C++, case is important, therefore
    Code:
    stclass[i]=ReadAllStudent();
    is wrong.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You will find people a lot more willing to help if you provide code that compiles.

    Some general notes:
    1. void main is wrong: Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])
    2. gets is bad: Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows
    3. Never fflush(stdin): Cprogramming.com FAQ > Why fflush(stdin) is wrong
    4. I think you are confused on the layout of your structure. You have:
    Code:
    struct Student
    {
    	char Name[30][50];
    	float grades;
    	int nbstudent,Max;
    	struct Date Birthdate;
    };
    Which is like saying each Student has 30 names of up to 50 chars, a grade, the total number of students, the maximum (age) and a birthday. Make your Student structure reflect a single student only, then make an array of these structures:
    Code:
    struct Student {
        // first name
        // last name
        // birthday
        // grade
    };
    ...
    int main(void)
    {
        int num_students = 0, max_age = 0;
        struct Student students[30];
    
        num_students = ReadNBStudent();
        for (i = 0; i < num_students; i++) {
            ...
        }
        ...
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K&R Learning Issue
    By TimHarrington in forum C Programming
    Replies: 48
    Last Post: 09-06-2010, 04:33 AM
  2. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  3. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  4. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM