I am trying to make a code that reads from a txt file the names, genders, current relationships and current school. I try to identify if a letter in the txt file is for example "z" (as in a Woman) but every time it gets to strcmp it crashes and outputs with -1073741819
I've tried different methods of inputting stuff into strcmp, like using "" instead of char. That has not worked out though.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *f;
if ((f=fopen("data.txt","r"))==NULL)
{
printf("File cannot be opened or does not exist");
}
else
{
char data[20],girl_c;
int girl,high,college,divorced;
girl_c = 'z';
girl=0;
high = 0;
college = 0;
while (!feof(f))
{
fgets(data,20,f);
printf("I've just read: %s\n",data);
if (strcmp(girl_c,data) == 0)
{
printf("Its a woman.");
}
}
if (high > college) //this is the end part of the code that i
premade, of course its not functioning
yet
{
printf("There are more divorced highschool girls (%d) than college girls (%d).",high,college);
}
else if (college > high)
{
printf("There are more divorced college girls (%d) than highschool girls (%d).",college,high);
}
else if (college == high)
{
printf("There is equal amount of divorced college (%d) and highschool girls (%d).",college,high);
}
else
{
printf("Data provided is insufficient or wrong.");
}
}
}
//code not yet finished as it cannot go past the strcmp and unless i fix its not worth going further
The problematic part is:
char data[20],girl_c;
int girl,high,college,divorced;
girl_c = 'z';
girl = 0;
high = 0;
college = 0;
while (!feof(f))
{
fgets(data,20,f);
printf("I've just read: %s\n",data);
if (strcmp(girl_c,data) == 0)
{
printf("Its a woman.");
}
}
It crashes on the strcmp.
the txt file is here: https://pastebin.com/T0FeKTwJ (Name; m = man, z = woman; s = single, v = married, r = divorced; z = elementary, s = high school, v = college)
As I said it goes to line 23 and then crashes and outputs with
Process returned -1073741819 (0xC0000005) execution time : 1.202 s
Press any key to continue.