I'm completely new here (19 year old webgeek taking her first dive into C at Mount Holyoke College), so play nice =)
So I'm reading a text file created in the first program I had to write into a second program I'm attempting to finish. What I need to do is give the average income, weight and age, as well as the names of any people whose age is above the average. That's fine. But what I'm stuck on is how exactly to get a count of the total number of people that I have information for, so I have a number to divide by in order to get the averages. Yes, this is a homework assignment - I'm not asking for it to be done for me, I just need a clue on how to do it. Any help I can get would be wonderful.
Code:
#include <stdio.h>
#include <string.h>
typedef struct
{
char name[25];
char ssn[12];
char city[50];
int age;
int weight;
char eyes[5];
double income;
} people_t;
int main (void)
{
int i=0;
int temp;
people_t x[25];
FILE *pFile;
if (( pFile = fopen("data.dat", "r")) == NULL)
printf("\nCannot open file\n");
for (i = 0; (i < 25); i++)
{
fscanf(pFile,"%[^\n]\n", &x[i].name);
fflush (stdin);
fscanf(pFile,"%[^\n]\n", &x[i].ssn);
fflush (stdin);
fscanf(pFile,"%[^\n]\n", &x[i].city);
fflush (stdin);
fscanf (pFile, "%d", &x[i].age);
fflush (stdin);
fscanf (pFile, "%d", &x[i].weight);
fflush (stdin);
fscanf(pFile,"%s", &x[i].eyes);
fflush (stdin);
fscanf (pFile, "%lf\n", &x[i].income);
fflush (stdin);
}
fclose(pFile);
return 0;
}
Any help is greatly appreciated.