-2

the hang man has a problem please identify it?? the letters when typed the code shows 'not found'and the same letter has to be typed twice to get it accepted? and the chances to guess the letter decreases how to fix it?

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

   #define WORD_COUNT 3
   #define MAX_LENGTH 10

   typedef char string[MAX_LENGTH];

   void main(void) {

       string words[WORD_COUNT] = { "bird","fish","lion","ants","bear","deer","fowl" };

       char answer[MAX_LENGTH];
       char guess;
       int count = -0, index, i, found, choice = -7;
       char mysteryWord[MAX_LENGTH];

       printf("Welcome to Hangman!\n");

       printf("\n\nChoose an option\n"
                "1) Easy\n"
                "2) Moderate\n"
                "3) Hard\n"

                "Your choice: ");

       scanf("%i", &choice);              a biref menu case
       switch (choice) {
       case 1:
           count = 5;
            break;
       case 2:
           count = 2;
           break;
        case 3:
           count = 1;

       }

       srand(time(NULL));
       index = rand() % WORD_COUNT;

       strcpy(mysteryWord, words[index]);/*actual comparing */
        for (i = 0; i < strlen(mysteryWord); i = i + 1)

       {
           answer[i] = '-';
       }

        answer[i] = '\0';

        printf("%s \n", answer);

        while (1 > 0) {
            printf("\n %i guess(es) left\n", count);
            printf("Guess a letter:");
            scanf("%c\n", &guess);
            guess = tolower(guess);

            found = 0;

            for (i = 0; i < strlen(mysteryWord); i++) 
            {
                if (mysteryWord[i] == guess) {
                    answer[i] = guess;
                    found = 1;
                }
            }
            if (found == 0) {
                printf("Not found!\n");
                --count;
            } 

            if (count == 0) {
                printf("Game over\n");
                printf("The answer is %s.", mysteryWord);
                break;
            }

            else {
                what should be here instead of if(answer==mysteryWord) ?
                if (strcmp(answer, mysteryWord) == 0) 
                {
                    printf("Yes, it's a %s\n", answer);
                    break; /* or return */
                } else
                    printf("%s", answer);   
            }
        }  end of while loop ?

    }  end of main ?
  • 3
    Welcome to StackOverflow. Please try debugging your program first. This is not a "here's my code, fix it for me" type site. This site is about answering specific programming questions. Please read [this](http://stackoverflow.com/help/how-to-ask). – Reticulated Spline Dec 25 '14 at 14:27
  • possible duplicate of [How to do scanf for single char in C](http://stackoverflow.com/questions/13542055/how-to-do-scanf-for-single-char-in-c) – Rizier123 Dec 25 '14 at 14:28

2 Answers2

1

Change

  scanf("%c\n", &guess);

To

  scanf(" %c", &guess);

Note the space before %c. The space discards all blanks like newlines and spaces and the %c will then scan the next non-whitespace character.

In your case,when you input data for any scanf,you enter the data and press the enter key.scanf reads the data entered and leaves the \n(newline character) in the stdin. When you scan a character using %c , scanf reads the \n left out by the previous scanf and thus,does not wait for input.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
1
scanf(" %c", &guess);

Please make sure your scanf() is like above with a space before %c

The purpose of space is it gobbles whitespace and special characters

Gopi
  • 19,784
  • 4
  • 24
  • 36