Here is where I take data from file and put it in structure.
Code:
struct NameRecord{
char name[31];
int year;
int frequency;
}records[200000];
Code:
fp=fopen("malebabynames.csv","r");
if (fp == NULL){
printf("Cannot open the malebabynames.csv file\n");
}else{
while (fgets(line_buffer, sizeof(line_buffer), fp)){
// printf("%s\n",line_buffer);
str = strtok(line_buffer,",");
records[currSize].year = atoi(str);
str = strtok(NULL,",");
strcpy(records[currSize].name,str);
//records[currSize].name = str;
str = strtok(NULL,"\n");
records[currSize].frequency= atoi(str);
// printf ("Years: %d\n", records[currSize].year);
currSize++;
}
fclose(fp);
Code:
void setNameYearTotals (char theName[], struct NameRecord records[], int size, int nameTotal[]){
int a;
int b,c;
int x;
int year = 1920;
int total;
char name[31];
//char str[31];
//printf("%s\n",records[size].name);
for (a=0 ; a < 18 ; ++a){
for (b=0; b < 5 ; ++b) {
total = 0;
year++;;
for ( x = 0; records[x].year != -1; x++){
//printf("%s\n",records[x].name);
if ( year == records[x].year){
//printf("Name: %s, Data name: %s\n",theName, records[x].name);
if ( theName == records[x].name){ //THIS IF STATEMENT IS NOT RUNNING
total = total + records[x].frequency;
printf("Total: %d\n",total);
}
}
}
//printf("Test: %s\n",name);
}
nameTotal[a] = total;
//printf("%d\n",yearRangeTotal[a]);
//printf("year: %d\n",year);
}
return;
}
I tried to compare a string from user input with a structure but for some reason it not working.
Code:
if ( theName == records[x].name){
total = total + records[x].frequency;
printf("Total: %d\n",total);
}
For example,
theName = "ERIC" and records[0].name contain "ERIC". However, the if statement say they are not the same.
I store records structure using strcpy and the user input is char array.
I also tried:
Code:
char name[31]
name = records[x].name;
It gives me error:
assignment3.c:155:8: error: incompatible types when assigning to type ‘char[31]’ from type ‘char *’
Thank you.