delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/04/03:45:28

Message-ID: <34FCFF2A.EC20BEBE@osu.edu>
Date: Wed, 04 Mar 1998 02:13:46 -0500
From: Jeff Farris <farris DOT 15 AT osu DOT edu>
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Loop help
References: <6dikoa$gel AT alpha DOT delta DOT edu>
NNTP-Posting-Host: 206.183.228.106
Lines: 48
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019