what am i missing in the game function and is my guessed array setup is right or do i need to put *guessed. also why is it not stopping after length of word in the while loop. also it loops for ever and when you get it right the first time then all letters becomes right after.
Code:
#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;
int hit = 0;
while (x < length && life > 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");
}
}
}