So, I'm a bit stuck here.
I'm supposed to make a program which will read data from a file to determine how many female and male adults of legal age exist in the file.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char b[1000];
int sum;
FILE *fptr;
if ((fptr = fopen("adults.txt","r")) == NULL)
{
printf("Error! opening file");
exit(1);
}
while (EOF != fscanf(fptr, "%[^\n]\n", b))
{
printf("%s\n", b);
}
fclose(fptr);
return 0;
}
So the output from the file looks something like this:
female 29
male 30
female 24
male 25
male 18
female 19
As for the determining the genders:
total females: 3 (whatever amount from the list in the file).
total males: 3 (whatever amount from the list in the file).
So, as you can see I'm fine with the output of the file in the input, but I'm not sure how to determine the amount of genders.
you need to read into 2 variables gender and age both, when you do the scan..
int males = 0;
int females = 0;
const int legal = 18; //or whatever it is. we drinking, voting, driving, or something else?
...
while(things)
{
if(age >= legal)
{
if(strcmp(something, "male") == 0 )
male++;
else
female++; //if you trust your file and code, you can take this shortcut. if not be explicit.
//print something here ?? here, the age is legal, so you can print ..
}
}
//or print something here, total # of each that is legal perhaps summary?