Logo 
Search:

C Programming Articles

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

Program to generate Summary of a survey

Posted By: Adalrico Fischer     Category: C Programming     Views: 2145

Suppose that a population survey has been carried in the given city, and that the information received from the survey has been transcribed in to punch cards.Since the cards have 80 cols each, one card contains the name, address,sex, age, profession etc., of one employee. That is , each card contains one record pertaining to one employee. Our problem is to print the details of all the adults in the given city under survey. Finally we also want to print total number of adults. Assume suitable sentinel value for any field in the trailer record and Write a program using structures/file to perform the job.

Code for Program to generate Summary of a survey in C Programming

#include <stdio.h>
#include <conio.h>
#include <stdio.h>
#include "valid.c"struct pop_detail
{  int no;
   char p_name[10];
   int p_age;
   int p_sexcode;
   char address[30];
};

 void getWriteFileName(char *);
 void getReadFileName(char *);
 void enterData();
 int getSexCode();
 void getTotalCount();
 void getName(char *);
 void getAddress(char *);

 FILE *fp, *rpt;
 int flag=1;
 int count=0;
 int adult_count=0;

 char filename[14];

 struct pop_detail pop_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);
       getTotalCount();
       }

    getch();
 }

 void enterData()
 {
    printf("\nEnter data ..... Use value 9999 for record no to terminate data entry.");
    while(flag)
    {
    //printf("\nEnter employee no. : ");//scanf("%d", &pop_cur.no);
    getPosInteger("Enter record no. : ", &pop_cur.no);
    fflush(NULL);

    printf("\nEnter Person name : ");
       //scanf("%[^\n]", pop_cur.p_name);
    getName(pop_cur.p_name);
    fflush(NULL);

    //printf("\nEnter employee age : ");//scanf("%d", &pop_cur.p_age);
    getPosInteger("Enter Person age : ", &pop_cur.p_age);
    fflush(NULL);

    printf("\nEnter Person sex (1 Male / 2 Female)  : ");
    pop_cur.p_sexcode = getSexCode();
    //printf("Sex code : %d", pop_cur.p_sexcode);

    fflush(NULL);
    printf("\nEnter Person Address : ");
    getAddress(pop_cur.address);
    fflush(NULL);

    fprintf(fp,"%-4d %-10s %-2d %1d %s", pop_cur.no, pop_cur.p_name, pop_cur.p_age, pop_cur.p_sexcode, pop_cur.address);


    flag++;

      /*if(pop_cur.no != 9999)
{ printf("\nEnter 1 to continue and 0 to exit.");
scanf("%d", &flag);
fflush(NULL);
}
else flag=0;
*/
if(pop_cur.no == 9999) flag = 0; else fprintf(fp,"\n"); } fclose(fp); //printf("\nTotal number of adult is : %d", adult_count); } void getName(char *str) { int count1=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; count1++; getstr++; if( count1 >= 10 ) break; } fflush(NULL); if(errorflag) getName(pop_cur.p_name); *getstr='\0'; } void getAddress(char *str) { int count1=0; char *getstr; char readch='\0'; fflush(NULL); getstr = str; while(1) { scanf("%c", &readch); if( readch == '\n') break; else *getstr = readch; count1++; getstr++; if( count1 >= 30 ) break; } fflush(NULL); *getstr='\0'; } int getSexCode() { int code; int rval; rval =scanf("%d", &code); fflush(NULL); if( ( rval == 0) || ( ( code != 1) && (code != 2) ) ) { printf("\n Invalid sex code entered. Enter 1 for male and 2 for female. :"); code = getSexCode(); } return code; } void getWriteFileName(char *fname) { printf("Enter Output File name maxium 14 character : "); scanf("%s", fname); if( ( fp = fopen(fname, "wt") ) == NULL ) { fclose(fp); printf("\nInvalid file name entered."); } else enterData(); } void getReadFileName(char *fname) { printf("Enter Input File name maxium 14 character : "); scanf("%s", fname); if( ( fp = fopen(fname, "r+t") ) == NULL ) { fclose(fp); printf("\nInvalid file name entered."); } } void getTotalCount() { flag =0; adult_count=0; if( ( rpt = fopen("prog10.rpt", "w+t") ) == NULL ) { fclose(fp); printf("\nInvalid file name entered."); exit(1); } while( feof(fp) == 0 ) { fscanf(fp, "%d %10s %d %d %[^\n]", &pop_cur.no, pop_cur.p_name, &pop_cur.p_age, &pop_cur.p_sexcode, pop_cur.address); printf("%-4d %-10s %-2d %1d %-30s\n", pop_cur.no, pop_cur.p_name, pop_cur.p_age, pop_cur.p_sexcode, pop_cur.address); fprintf(rpt,"%-4d %-10s %-2d %1d %-30s\n", pop_cur.no, pop_cur.p_name, pop_cur.p_age, pop_cur.p_sexcode, pop_cur.address); if(pop_cur.p_age >= 18) adult_count++; } fclose(fp); printf("\nTotal number of adult is : %d", adult_count); } /****************************** Input ********************************************/
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 ..... Use value 9999 for record no to terminate data entry. Enter record no. : 1 Enter Person name : chintan Enter Person age : 27 Enter Person sex (1 Male / 2 Female) : 1 Enter Person Address : sabarmati /********* Output ********

1 Jayesh 22 1 sabarmati
2 nilay 22 1 vastrapur
9999 ket 22 2 maninagar

*********************************/
  
Share: 


Didn't find what you were looking for? Find more on Program to generate Summary of a survey Or get search suggestion and latest updates.

Adalrico Fischer
Adalrico Fischer author of Program to generate Summary of a survey is from Frankfurt, Germany.
 
View All Articles

 
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!