Mail Archives: djgpp/1998/03/04/03:45:28
Matt Kris wrote:
>
> I know this isn't a DJGPP question but I dunno the C group.
>
> I am trying to start on a Hangman game. Not I have no idea why this won't
> work. It's suppose to take a word that is stored in an array and check if
> the guess matches any of the letters in the word. I had it working once but
> I changed something and don't remember what!! Please take a look.
>
> int turns=6; /* Number of turns before man is
> dead */
> char word[25]="monkey"; /* The actual word */
> int length=6; /* The length of the word */
> int counter; /* The counter */
> char guess; /* Holds the guesses */
>
> main()
> {
>
> /* The hard part that needs worked on */
>
> while(turns>0)
> {
> printf("\nYou have %d turns left\n", turns);
> printf("\nGuess? ");
> scanf("%c", guess);
> for(counter=length;counter>0;counter--)
> {
> if(guess==word[counter])
> {
> printf("You got one\n");
> printf("You got %c", word[counter]);
> turns++;
> }
> }
> turns--;
> }
> return 0;
> }
In C, arrays begin at index 0 and end at index (length - 1) so you want
the for loop to iterate from counter=5 to counter=0 instead of 6 to 1.
Also, the second argument to scanf should be an address, ie '&guess', or
'word' (w/o quotes :).
Jeff Farris
- Raw text -