delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/04/06:42:49

From: G DOT DegliEsposti AT ads DOT it
To: djgpp AT delorie DOT com
Message-ID: <C12565BD.003F790B.00@vega.ads.it>
Date: Wed, 4 Mar 1998 12:41:27 +0100
Subject: Re: Re: Loop help
Mime-Version: 1.0




> I know this isn't a DJGPP question but I dunno the C group.
IIRC there are two: comp.lang.c and comp.lang.c.moderated

> char word[25]="monkey";    /* The actual word */
> int length=6;                          /* The length of the word */
> int counter;                            /* The counter */
> char guess;                          /* Holds the guesses */
[...]
>      printf("\nGuess? ");
>      scanf("%c", guess);

scanf expects the address of the variable, so you have to write: &guess

don't worry if you don't see the prompt before you type in the guess,
this is because stdout is line buffered, you can use fflush(stdout) to
force output of the buffer.

>      for(counter=length;counter>0;counter--)
>        {
>          if(guess==word[counter])

There is another problem you didn't notice here: string in C begin at
index 0, while your code assumes 1.

This code will check value word[6] which should be the '\0' string
terminator
and it will not check word[0].

Use word[counter -1] or better loop from lenght -1 down to 0

ciao
  Giacomo




- Raw text -


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