delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/09/02:31:40

From: "The Brewer Family" <brewer AT sonic DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Loop help
Date: 9 Mar 1998 07:21:37 GMT
Organization: Sonic,Santa Rosa CA,http://www.sonic.net
Lines: 40
Message-ID: <01bd4b2c$012b48a0$31e5c9d0@the-beast>
References: <6dikoa$gel AT alpha DOT delta DOT edu>
NNTP-Posting-Host: d49.pm.sonic.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

> 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--)

	It should work some of the time, at least. Remember, arrays start at 0,
not one. If you want to check for the first element in the array, you
should check that counter >= 0. Also, checking word[length] is checking one
beyond the end of the array, in this case just a null character at the end
of the string. 

>        {
>          if(guess==word[counter])
>            {
>              printf("You got one\n");
>              printf("You got %c", word[counter]);
>              turns++;
>            }
>        }
>      turns--;
>    }
>  return 0;


- Raw text -


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