0

I'm creating a program that asks the user to input a word. The word is then compared with a word in a text file. If correct, I want the user to input another word which should correspond with the next word in the text file and this should loop until the end of the file. I'm having trouble with the loop to the end of the file. Could someone please review my code and give me a few pointers? thanks so much

#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;
}

2 Answers 2

1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
    FILE *fp = fopen("secretwords.txt", "r");   
    if (fp == NULL)
    {
        printf("Error reading file\n");
        return 1;   
    }

    char guess[20] = {0};
    char secret[20] = {0};
    while(fscanf(fp, "%s", secret) != EOF) // i would suggest you use 'fscanf_s("%s", guess);' instead if available
    {
        printf("Please guess the word: \n");
        scanf("%s", guess); // i would suggest you use 'scanf_s("%s", guess);' instead if available

        if (!strncmp(secret, guess, sizeof(guess)))
        {
            printf("Your guess was correct. Continue ...\n");           
        }
        else
        {
            printf("Your guess was incorrect. Good bye.\n");
            break;
        }
    }
    fclose(fp);

    return 0;
}

i made some suggestions about scanf_s and fscanf_s, if they are available, use them. But still, i am wondering why they are still teaching bad code in schools? I would not suggest to use *scanf* functions at all. Further reading: uncontrolled format string

Sign up to request clarification or add additional context in comments.

Comments

0
  • Move the fscanf call that reads from the file to a function that returns the next word
  • loop for user input, only calling the function outlined above when you need to advance to the next word in the file (when the user inputs the correct thing)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.