Hello all,

I'm trying to read in a group of letters each on a new line and have the user guess them depending on how many games they want to play. My program works fine but I've noticed while testing it that for any more attemps, fscanf just keeps the first letter even though its in a loop.

How can I get the pointer to keep moving through the file ? I thought the for loop would do this?

I googled for some answers and got suggestions for flushall() and it didn't help. Before I added the flushall() however, it wasn't even giving me a letter for the second or third game...now its stuck at r at least.

Here is my code where this is occurring:

    for(i=0;i<gamesToPlay;i++)
    {
        //get a letter from file
        fscanf(inp,"%c", &file_letter);
        printf("current letter: %c", file_letter);
        flushall(); 

        //Play one game
        result = PlayGuess(file_letter);
        //check for win or lose 
        if (result == 1)
        {
        printf("Expect a check in the mail...\n");
        }

        else if (result == 0)
        {printf("You lost....looser!\n\n");
        }

    }

    //close file
    fclose(inp);
    return 0;
}

Thanks!

P.S. The file its reading from looks like this:
r
a
y
n
...

Recommended Answers

All 3 Replies

The problem is fscanf() isn't removing the newline character because you never told it to do that. Make this change to your program.

fscanf(inp,"%c\n", &file_letter);

That did it ...I just got rid of the flushall(); as well.

Thanks a bunch !!!

Glad to help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.