user3715718
user3715718

Reputation:

Letter guessing game weird output

So here is my code:

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h> 
#define MAX_GUESSES 4
int PlayGame(char guess);
int WinOrLose(char userguess, char solution);
int main()
{
        FILE* infile;
    char correctlet;
    int games,
        igame,
        result;
    infile = fopen("inputLet.txt", "r");
    printf ("Welcome to the letter guessing game!\n");
    printf ("Your job is to guess a random letter.\n");
    printf("You can guess each letter a maximum of 4 times,\n");
    printf ("and you will get a hint after every guess.\n");
    printf ("LET'S DO THIS!\n\n>");
    printf ("\nHow many games would you like to play (1-3)?\n>");
    scanf ("%d",&games);
    for(igame=0;igame<games;igame++)
    {
        fscanf(infile," %c",&correctlet);
        printf("This is game %d\n", igame+1);
        result = PlayGame (correctlet);
        if (result == 0)
        {
            printf ("\nCongratulations, you guessed the right letter!\n");
        }
        else
        {
            printf ("\nUnfortunately, you did not guess the right letter. Better luck next time!\n");
    }
    }
    return 0;
}
int PlayGame(char solution)
{
    int guessnumber,
        result;
    char userguess;
    guessnumber = 0;
    while(guessnumber < MAX_GUESSES)
    {
        printf("Please enter your guess\n>");
        scanf("%c", &userguess);
        if (sizeof userguess == 0)
        {
            continue;
        }
        else if (sizeof userguess >=1)
    {
        printf ("Your guess was %c\n",userguess);
        result = WinOrLose (userguess, solution);
        if (result == 0)
        {
            return 0;
            break;
        }
        else if (result == 1)
        {
            if (solution < userguess)
            {
                printf("The correct letter comes before %c alphabetically\n", userguess);
            }
            else if (solution > userguess)
            {
                printf("The correct letter comes after %c alphabetically\n", userguess);
            }
        guessnumber ++;
        }
    }
}
}
int WinOrLose (char userguess, char solution)
{
    if(solution==userguess)
    {
        return 0;
    }
    else if (solution != userguess)
{
    return 1;
}
}

The output asks for the number of games, and then it outputs please enter your guess your guess was (blank) The correct letter comes after (blank) Please enter your guess and THEN it allows for user input. So why is it going through one iteration of PlayGame without asking for user input? I have tried everything I can think of and can't fix the problem. I am compiling on VC++ 2010, if that helps. Thanks in advance!

Upvotes: 0

Views: 638

Answers (1)

Happington
Happington

Reputation: 454

The simple answer is to flush your buffers.

The stdin buffer, the buffer that takes instructions from the keyboard (or a pipe) and submits it to the program occasionally gets some characters "stuck" in it. Junk characters that never quite get submitted, extra returns, etc. that will cause scanf() to think it reached the proper end, but actually hasn't.

fflush(stdin);

The function fflush "flushes" a buffer. The effect of this is to consume data from a buffer until the data received is the character '\0' (NULL). This means that it's reached the last of the data that is currently in the buffer.

Calling this before calling scanf() means that when scanf() is called, you reasonably know that the program will block on scanf() until you've submitted, and not just consume some junk from the buffer.

Upvotes: 2

Related Questions