0

I need to have a user input a word then compare the word with a text file to see if it is correct. The user has 3 attempts to enter the word before the program terminates. My issue is reading the word from the file I know it's something simple that I have wrong. I should also clarify that the error I'm getting is in the compiler I haven't gotten to the point of being able to compare the strings yet!

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

int main(void)
{
    FILE *fp;
    fp = fopen("secret.txt","r");
    char guess[10];
    const char secret[10];
    int i, c;
    c = getc(fp);
    fgets(secret, sizeof(secret), fp);

    for (i=0; i < 3; i++)
    {
        printf("Please guess the word: \n");
        scanf("%s", guess);

        while (c !=EOF)
        {   
            if (strcmp(secret,guess)==0)
            {
                printf("Your guess was correct");
                return 0;
            }
            else
            {
                printf("Your guess was incorrect. Please try again\n");
            }
        } 
        fclose (fp);
    }
    return 0;
}
2
  • What's the reason for having c = getc(fp)? Commented Nov 30, 2011 at 15:34
  • So that the program loops until it has read all of the characters in the file. That's the purpose of the while loop in my code. Commented Nov 30, 2011 at 15:36

4 Answers 4

4

Here are some pointers:

  1. c = getc(fp) consumes the first character of the file, so it never becomes part of the secret variable.
  2. If secret.txt contains a newline, the newline is read into the secret variable.
  3. The while (c != EOF) loop seems pointless, since c isn't modified inside the loop. Furthermore, the infinite nature of the loop prevents the outer for loop from functioning correctly.

If I were you, I'd fix the while loop and would make sure that secret is read correctly, for example by printing it out or examining it in a debugger.

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

1 Comment

I've gotten rid of the while loop thereby eliminating that problem but my issue is at the compiler stage. I'm getting issues trying to retrieve the string from the text file. Thanks so far!
2

What is

c = getc(fp);

needed for? My "guess" would be that you read the first character of the word into c and then secret misses the first character.

EDIT: Instead of using getc for EOF checking, which as said corrupts the read word (and this while loop is rubbish anyway), just check the return value of fgets:

if(fgets(secret, sizeof(secret), fp) == NULL)
    //file is empty or other error occurred

and remove this infinite while(c != EOF) loop.

So it should rather look something like:

FILE *fp = fopen("secret.txt","r");
char guess[10];
const char secret[10];
int i;
if(fgets(secret, sizeof(secret), fp) == NULL)
{
    printf("Error while reading file\n");
    return -1;
}
fclose(fp);

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");
        return 0;
    }
    else
        printf("Your guess was incorrect. Please try again\n");
}
return 0;

Comments

1

Your code is grossly off: you do not alter 'c' inside a loop, making it spin indefinitely. It's a good idea to sketch your algorithm on a piece of paper before you start coding. In your case, pseudocode should look like this:

  • Open file
  • Read the secret
  • Close file
  • Repeat three times:
  • --- Display the prompt
  • --- Read user input
  • --- If user input matches the secret, congratulate the user and exit.
  • Tell the user his guess was incorrect.

At this point, converting it to C should be more or less mechanical. Good luck!

Comments

0
while (c !=EOF)
{   
    if (strcmp(secret,guess)==0)
    {
        printf("Your guess was correct");
        return 0;
    }
    else
    {
        printf("Your guess was incorrect. Please try again\n");
    }
} 

looks like an infinite loop to me

3 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
@SamIam a comment is not sorce-code-formattable. And there is definitle a flaw in the code, which should be pointed out. (and the answer could have forced the OP to think again about his design) But it is nearly one Year ago, since I wrote the answer, so I do not know the "Werdegang" any more.
Your answer showed up in the low-quality posts queue, and that comment is an auto-generated because I chose the option that it wasn't a real answer, because it's not.

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.