Good day, I am having a pointer error in my string compare. can somebody please correct me? the program is reading have a rough text file of
abcde
ebcad
acdbe
and several same letters of the different orders and check which one is equal to the first one. I got the comparison right and it produces
ae
bb
cc
da
ed
and
aa
bc
cd
db
ee
so the output should be
bb
cc
and
aa
ee
but it's give me an error.
Code:
int main(void)
{
FILE *fp;
char *status, *status2;
int times;
int looptimes;
struct
{
char numberofItems[2];
char answersToQuestions[50];
} answerKey[SIZEANS];
struct
{
char studentID[7];
char answers[50];
} answer[SIZE];
int y, szAns[] = {0,3,51};
int i, sz[] = {0,6,51};
char buff[1024];
char buff2[1024];
char insert[1024];
int x = 0;
int w = 1;
fp = fopen("Exam.txt","r");
status = fgets(buff, sizeof(buff), fp);
status2 = fgets(buff2,sizeof(buff2),fp);
for(times = 0; times<1;times++)
{
strncpy(answerKey[times].numberofItems, strtok(buff," "), szAns[1]);
strncpy(answerKey[times].answersToQuestions,strtok(NULL," "), szAns[2]);
printf("%s %s",answerKey[times].numberofItems,answerKey[times].answersToQuestions);
for(w = 0; w <= strlen(answerKey[times].answersToQuestions) - 2; w++)
{
printf("%c\n",answerKey[times].answersToQuestions[w]);
}
}
while(status2 != NULL)
{
strncpy(answer[i].studentID, strtok(buff2," "),sz[1]);
strncpy(answer[i].answers, strtok(NULL," "),sz[2]);
printf("%s",answer[i].answers);
for(x = 0; x<=strlen(answer[i].answers) - 1; x++)
{
if(strcmp(answer[i].answers[x], answerKey[0].answersToQuestions[x]) == 0)
{
printf("%c",answer[i].answers[x]);
printf("%c\n",answerKey[0].answersToQuestions[x]);
}
}
x = 0;
i++;
status2 = fgets(buff2,sizeof(buff2),fp);
}
printf("\n");
fclose(fp);
return 0;
}