Program to store family members details, C/C++ Programming

Assignment Help:

Program to store family members details in C++:

#include

#include

#include

struct record

{

  char name[30];

  int age;

  char state[40];

}x;

//calculates average of ages all family members

float  averagecalculation(char sr_file[20])

{

 float avg;

 int totalmembers=0,totalage=0;

 FILE *fp1;

 fp1=fopen(sr_file,"r");

 if(fp1==NULL)

 {

   printf("cannot read file %s",sr_file);

   exit(0);

 }

 while(fscanf(fp1,"%[^\n]%d%[^\n]",&x.name,&x.age,&x.state)!=EOF)

 {

totalmembers++;

 totalage=totalage+x.age;

 }

 fclose(fp1);

 avg=(float)totalage/totalmembers;

 return avg;

 }

 //prints family members names who live in TEXAS

 void print_members_of_texas(char sr_file[20])

 {

 

 FILE *fp1;

 fp1=fopen(sr_file,"r");

 if(fp1==NULL)

 {

   printf("cannot read file %s",sr_file);

   exit(0);

 }

 printf("\nFamily members who live in Texas are\n");

 while(fscanf(fp1,"%[^\n]%d%[^\n]",&x.name,&x.age,&x.state)!=EOF)

 {

  if(strcmp(x.state,"TEXAS")==0)

     printf("\n%s",x.name);

  }

  fclose(fp1);

}

void main()

{

   FILE* fp1;

   char sr_file[20];

   float avg;

   clrscr();

   printf("Enter the source file name to enter data");

   gets(sr_file);

   fp1=fopen(sr_file,"w");

   if(fp1==NULL)

   {

     printf("cannot create %s ",sr_file);

     exit(0);

   }

   while(1)

   {

    fflush(stdin);

    printf("Enter name of ur family member(-1 to quit)  ");

    scanf("%[^\n]",x.name);

    if(strcmp(x.name,"-1")==0)

       break;

    fflush(stdin);

    printf("Enter his age   ");

    scanf("%d",&x.age);

    if (x.age<1)

      {

       printf("invalid input  ");

       continue;

      }

    fflush(stdin);

    printf("Enter state where he lives   ");

    scanf("%[^\n]",x.state);

    fprintf(fp1,"%s\t%d\t%s\n",x.name,x.age,x.state);

 }

 fclose(fp1);

 printf("Done reading input");

 //Now calculate average

 avg=averagecalculation(sr_file);

 printf("\nAverage age  of family is %f ",avg);

 //Now check if any family member is in Texas

  print_members_of_texas(sr_file);

  getch();

 }

PROGRAM  Description

 Program has 3 functions.

1.Main()- creates a file to store family members details .It opens the file in write mode. Its fields are name of member ,age and where he lives. Then it takes input into the structure record fields called name ,age ,state till user presses -1. It checks if age entered is >1 or not ,otherwise it gives "invalid input"Then it completes taking input by closing the file. After that it  calls 2 functions named averagecalculation() and print_members_of_texas() for calculating the average of family members age and to print the family members who live in TEXAS respectively.

2.averagecalculation()- It is a function to calculate the average of all family members. It is started by opening the file created in main function in read mode. It reads each record of file stored in sequence till end of file. It  calculates average of ages. Lastly closes the file and returns to main() with average.

3.print_members_of_texas()-It is started by opening the file created in main function in read mode. It reads each record of file stored in sequence till end of file. Then it check each record if person's city is TEXAS or not, if it is so this function prints the person's name.  Lastly closes the file.


Related Discussions:- Program to store family members details

Static data members and static member function, Static Data Members: A ...

Static Data Members: A data member inside the class can construct as static data member.   There are few guidelines to be followed when declaring static data member.

Wap to print series from 1 to 10 & find its square and cube, WAP TO PRINT S...

WAP TO PRINT SERIES FROM 1 TO 10 & FIND ITS SQUARE AND CUBE # include stdio.h> # include conio.h> # include math.h>   void main () { int a=1,sqr=0,cube=0;

Hwid spoofer for windows 7, HWID Spoofer for Windows 7, 8 x64 Project De...

HWID Spoofer for Windows 7, 8 x64 Project Description: I want a simple program that will spoof my hardware id. I should be able to prepare the id to spoof to in the program a

What is the employ of ''using'' declaration?, A: A using declaration makes ...

A: A using declaration makes it possible to employ a name from a namespace without the scope operator.

Program for construct a struct - user defined data type, Define a user-defi...

Define a user-defined data type (struct) called Car to represent a car in the context of a car dealership database. Car must have at least the following characteristics: Make Model

Car rental project, I need a project on car rental system using c programmi...

I need a project on car rental system using c programming only of college level

Explain the symbolic constants in c language, Explain the Symbolic Constant...

Explain the Symbolic Constants in c language? Symbolic constants are the constants of any type that declared by using the #define compiler directive and it is a preprocessor di

#tit, Write a program to find the area under the curve y = f(x) between x =...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd