Я создаю программу, которая просит пользователя ввести слово. Затем слово сравнивается со словом в текстовом файле. Если все правильно, я хочу, чтобы пользователь ввел другое слово, которое должно соответствовать следующему слову в текстовом файле, и это должно повторяться до конца файла. У меня проблемы с циклом до конца файла. Может ли кто-нибудь просмотреть мой код и дать несколько советов? Большое спасибо
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
//Step 1: open file and declare variables//
FILE *fp;
fp = fopen("secretwords.txt","r");
char guess[20];
char secret[20];
int i, count;
//Step 2: Check that file opened correctly, terminate if not//
if (fp == NULL)
{
printf("Error reading file\n");
exit (0);
fclose(fp);
}
//Step 3: Create loop to run for each word to run to end of file//
fscanf(fp,"%s", secret);
//Need to create a loop here that will read the text file 20 times,
// each time reading the next word//
for (i=0; i < 3; i++)
{
printf("Please guess the word: \n");
scanf("%s", guess);
if (strcmp(secret,guess)==0)
{
printf("Your guess was correct\n");
return 0; //This return will terminate the program.
// I need to restart loop from here
}
else
{
printf("Your guess was incorrect. Please try again\n");
}
}
return 0;
}