1

kk i fixed it but there is still one statement i have to put i think which is if (hit == length) then outside the while loop printf you won. but my question now where should i put the if (hit == length)

#include <stdio.h>
#include <conio.h>
#include <string.h>

//function prototypes

void game(char [], char [], int);
int main()
{
    char word[20] = {'d', 'u','c','k'};
    char guessed[20] ={'*s', '*s','*s','*s' };
    int length = strlen(word);

    game(word,guessed, length);

    getch();
    return 0;   
} 
void game(char answer[], char guess[], int length)
{
    int life = 5;   
    int x = 0;
    int y = 0;
    char letter;

    while (x < length && life > 0){
      int hit = 0;
      printf("enter letter\n");
      scanf(" %c",&letter);   
      for (y = 0; y < length; ++y) {
          if (answer[y] == letter && guess[y] != letter) 
          {
              ++hit;
              guess[y] = letter;
          }
      }
      if (!hit) 
      {
          x += hit; 
          printf("try again\n");
          life = life - 1;
          printf("%d tries remaining \n", life); 
      }
      else {
          printf("keep going\n");
      }  
   } 
}
4
  • 3
    Best situation ever to use a debugger.
    – Maroun
    Apr 7, 2014 at 7:40
  • Also, formatting code correcly never hurts. Apr 7, 2014 at 7:43
  • 1
    What is '*s' supposed to do? Apr 7, 2014 at 7:43
  • one problem with questions about word games - their authors never explain the workflow of the game, but immediately start discussing the error
    – mangusta
    Apr 7, 2014 at 7:46

3 Answers 3

0

Your condition if (!hit) checks out if hit is 0 or not.

However, you initialize it to 0 only once, at the beginning of the procedure - and not for every iteration of your while(..) loop.
Therefore - once hit was set to be not 0 (in some iteration) it will always remain that way (not 0).

You should set hit = 0; inside the while loop (probably at its start).

0

You should set hit to 0 at the start of your while loop so it'll check hit seperately for every iteration of the for loop :P

0

try this

char guessed[20] ={ '*', '*','*','*' };
....
if (!hit) {
    printf("try again\n");
    life = life - 1;
    printf("%d tries remaining \n", life); 
}
else {
    x = hit; 
    printf("keep going\n");
}  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.