Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program using structures/file to print name and age of the oldest and the youngest student in the class

Posted By: Adelyte Miller     Category: C Programming     Views: 8974

Write a program using structures/file to print name and age of the oldest and the youngest student in the class. The input records contain the name and the age of the students. Assume the sentinel value of 99 for the age field of the trailer record.

Code for Program using structures/file to print name and age of the oldest and the youngest student in the class in C Programming

#include <stdio.h>
#include <conio.h>
#include "valid.c"struct student_detail
{
   char name[10];
   int age;
};

 void getWriteFileName(char *str);
 void getReadFileName(char *str);
 void findGSAge();
 void enterData();
 void getName(char *);

 FILE *fp;
 char filename[14];
 int flag=1;
 struct student_detail s_young, s_elder, s_cur;

 void main()
 {
    char option;
    clrscr();
    printf("\nDo you want to create the data file type 'y' for yes and 'n' for no.");
    scanf("%c", &option);
    fflush(NULL);
    if( option == 'y' || option == 'Y')
    getWriteFileName(filename);
    elseif( option == 'n' || option == 'N')
      {   getReadFileName(filename);
      findGSAge();
      }
    getch();
 }

 void enterData()
 {
      s_young.age = 99;
      s_elder.age = 1;
    printf("\nEnter data .....");
    while(flag)
    {
    printf("\nEnter student name : ");
    //scanf("%[^\n]", s_cur.name);
    getName(s_cur.name);
    fflush(NULL);

    //printf("\nEnter student age : ");//scanf("%d", &s_cur.age);
    getPosInteger("Enter student age : ", &s_cur.age);
    fflush(NULL);
    fprintf(fp,"%-10s %d", s_cur.name, s_cur.age);

    if( flag > 0 )
       fprintf(fp,"\n");

    flag++;
    if(s_cur.age != 99)
    {
       printf("\nEnter 1 to continue and 0 to exit.");
       scanf("%d", &flag);
       fflush(NULL);
    }
    else  flag = 0;

      if(s_young.age > s_cur.age)
      {   s_young.age = s_cur.age;
          strcpy(s_young.name, s_cur.name);
      }
      if(s_elder.age < s_cur.age)
      {   s_elder.age = s_cur.age;
          strcpy(s_elder.name, s_cur.name);
      }
    }
    fclose(fp);
      printf("\nYoungest student is : %-10s %d", s_young.name, s_young.age);
      printf("\nOldest student is : %-10s %d", s_elder.name, s_elder.age);
 }

    void getName(char *str)
    {
    int count=0;
    char *getstr;
    char readch='\0';
    int errorflag = 0;

       fflush(NULL);
       getstr = str;

       while(1)
       {
        scanf("%c", &readch);
        if( readch == '\n')
           break;
        if( isalpha(readch) == 0)
        {   printf("\nInvalid entry. Enter characters only.\nEnter name again :");
            errorflag = 1;
            break;
        }
        else
            *getstr = readch;

        count++;
        getstr++;
        if( count >= 10 )
          break;
       }

       if(errorflag)
          getName(s_cur.name);

       //*getstr='\0';
    }

   void getWriteFileName(char *str)
   {
     printf("Enter Output File name maxium 14 character : ");
     scanf("%s", str);

     if( ( fp = fopen(str, "wt") ) == NULL  )
     {  fclose(fp);
    printf("\nInvalid file name entered.");
     }
     else
    enterData();
  }

   void getReadFileName(char *str)
   {
     printf("Enter Input File name maxium 14 character : ");
     scanf("%s", str);

     if( ( fp = fopen(str, "r+t") ) == NULL  )
     {  fclose(fp);
    printf("\nInvalid file name entered.");
     }
   }

   void findGSAge()
   {
      s_young.age = 99;
      s_elder.age = 1;

      while( feof(fp) == 0 )
      {
      fscanf(fp, "%10s %4d", s_cur.name, &s_cur.age);
      if(s_young.age > s_cur.age)
      {   s_young.age = s_cur.age;
          strcpy(s_young.name, s_cur.name);
      }
      if(s_elder.age < s_cur.age)
      {   s_elder.age = s_cur.age;
          strcpy(s_elder.name, s_cur.name);
      }
      fflush(NULL);
      }

      fclose(fp);
      printf("\nYoungest student is : %-10s %d", s_young.name, s_young.age);
      printf("\nOldest student is : %-10s %d", s_elder.name, s_elder.age);
   }

    /*******************************  Input/Ouput ********************************/
Do you want to create the data file type 'y'for yes and 'n'for no.y Enter Output File name maxium 14 character : output.txt Enter data ..... Enter student name : nilay Enter student age : 44 Enter 1 to continue and 0 to exit.1 Enter student name : chintan Enter student age : 99 Youngest student is : nilay 44 Oldest student is : chintan 99 /*******************************************************************************/
  
Share: 



Adelyte Miller
Adelyte Miller author of Program using structures/file to print name and age of the oldest and the youngest student in the class is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!