Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6879209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:49:04+00:00 2026-05-27T04:49:04+00:00

I need to have a user input a word then compare the word with

  • 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:49:05+00:00Added an answer on May 27, 2026 at 4:49 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following requirement: Based on some user input, I need to generate
I need have a textbox where user can input latitude and longitude, eg. 51.499167,
I need to know which views does a user have which privileges on. I'm
i have a large user Database (13k+), and for some reason i need to
I have a need for unique reusable ids. The user can choose his own
I have 2 Java EE applications which need to share some common user related
I have a requirement wherein I need to display on a User form, a
I have a Middleman project and I need to emulate a logged-in user. Simple
I have a web application inside where i need to check whether the user
I have an input box: input type=text class=rulerInputText id=rulerInputBox readonly I have Javascript code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.