-1

I know this code is not completed yet, but I cannot go any further because of this issue.

If you execute the code with any compiler you will see it.

After the instructions gets written at console, when you enter a word, loop takes 2 turns. It reduces chance 2 times too when it's supposed to be 1. Why is that?

I am using devc++ and windows.

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

int main(){     
    int i,j,totalTrial=6,currentTrial=0;
    char myWord [6]={'d','o','c','t','o','r'};
    char lineArray [6]={'-','-','-','-','-','-'};
    char guess;     

    printf("Hello,this is a simple word-guessing game. Try to find my secret word. You have 6 chances.");

    printf("Lets begin!!\n");

    printf("Word:\n------\n");

    for(i=0;i<=6;i++)
    {
        printf("\nGuess a letter: ");
        scanf("%c",&guess);

            for(j=0;j<7;j++)
            {

                if(guess==myWord[j])
                {
                lineArray[j]=guess;
                }
            }

        currentTrial++;
        printf("\nResult: %s, %d hakkin kaldi.\n",lineArray,totalTrial-currentTrial);           
    }       

    getch();
    return 0;
}
ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • hakkin kaldi? Google Translate states that this is Hausa for "copyright". Is that the right string to use in this context, or is Google just brain-dead? – paxdiablo Jan 08 '19 at 01:13
  • "DevC++" says just nothing. That isn't a compiler. – Swordfish Jan 08 '19 at 01:27
  • @Swordfish: given it *bundles* gcc, I'd say that was a fairly safe bet as to what compiler it was using. It *can* be configured to use other compilers but that would be unlikely to have been done by a beginner, given the pain it often causes even to experienced developers :-) – paxdiablo Jan 08 '19 at 01:40
  • @paxdiablo I just want to teach n00bs the difference between IDE and compiler ^^ – Swordfish Jan 08 '19 at 01:49

2 Answers2

1

This is happening because the scanf() is reading the stray \n (newline character) from the input buffer. [When you are giving input, you must be entering a character followed by ENTER key.]

To resolve this, add a space before % character in scanf() like this:

scanf(" %c", &guess);

This will skip the leading whitespace characters (including newline character) and read the input given by the user.

H.S.
  • 11,654
  • 2
  • 15
  • 32
0

With regard to the line:

scanf("%c",&guess);

How many characters do you think are turning up when you enter, for example, dENTER? I'll give you a hint, it isn't one :-)

The problem is that your scanf will read each character in turn and process it, including the newline generated when you hit ENTER.

A better solution would be to use a more complete input solution such as this one here.

It will handle many scenarios that a simple method based on scanf or getchar.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • oh no :( when i take a word with scanf and count chars with strlen (apple for example), result comes as 5 so i thought it doesnt count enter itself. thanks a lot for the hint, any tip about how to fix this? – Elif Bahar Özdoğru Jan 08 '19 at 01:13
  • @ElifBaharÖzdoğru, assuming you're doing `scanf("%s")`, you're right that it stops at the first whitespace character (so doesn't include the `\n`). Using `%c` has different behaviour. In terms of fixing it, either of the (current) two answers will do. Prefixing your `%c` with a space (as per H.S. answer) is probably the easiest in your case while mine is more flexible for general line-based input. – paxdiablo Jan 08 '19 at 01:16
  • I appreciate the help really. We are having algorithm and c language classes around 3 months now and you provided more information in 3 minutes :) – Elif Bahar Özdoğru Jan 08 '19 at 01:30