Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is with how scanf() is used. Please use this [link] to see some details. </p> <p>if you repace : scanf( " %c", theLetter ) with: scanf( " %c", &amp;theLetter ) then everything will be OK.</p> <p>Full Working Code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;ctype.h&gt; // MAXWORD, which will be the max word length #define MAXWORD 20 // INCORRECT_GUESSES, which will be the max guesses #define INCORRECT_GUESSES 6 /* Prototypes */ // Fills theArray with howMany copies of theLetter void fill_array( char *theArray, int howMany, char theLetter ); // Get char from player, checks the letter, shows progress so far int get_letter( char *theWord, char *soFar ); // Check if letter is in word, updates progress so far int letter_in_word( char *theWord, char *soFar, char theLetter ); // Convert the word to lowercase void lower_string( char *someWord ); // Play one game void play( char *theWord ); /* Function definitions */ int main( ) { char theWord [ MAXWORD ]; FILE* word; word = fopen( "guesswords.txt", "r" ); if ( word == NULL ) { printf( "No input file found..........\n" ); return -1; } fscanf( word, "%s", theWord ); printf( "%s\n", theWord ); lower_string( theWord ); printf( "%s\n", theWord ); play( theWord ); fclose( word ); return 0; } // Get char from player, checks the letter, shows progress so far int get_letter( char *theWord, char *soFar ) { char theLetter; printf("\nPlease enter a letter: "); scanf( " %c", &amp;theLetter ); theLetter = tolower(theLetter); letter_in_word( theWord, soFar, theLetter ); return theLetter; } // Fills theArray with howMany copies of theLetter void fill_array( char *theArray, int howMany, char theLetter ) { int i; for( i=0; i&lt;howMany; i++ ) { theArray[i]= theLetter; *(theArray + i) = theLetter; *theArray = theLetter; } theArray[howMany] = '\0'; } // Check if letter is in word, updates progress so far int letter_in_word( char *theWord, char *soFar, char theLetter ) { int i; int num=0; int len = strlen(theWord); for( i=0; i&lt;len; i++) { if (theWord[i] == theLetter ) { soFar[i] = theLetter; num++; } } if (num == 0) { printf( "SORRY! your letter is not in the word\n" ); return 0; } else if (num&gt;0) { printf( "Congratz! your letter was in the word\n" ); printf("%s", soFar); return 1; } } // Convert the word to lowercase void lower_string( char *someWord ) { int i, cha; int len = strlen( someWord ); for( i=0; i&lt;len; i++ ) { cha = someWord[i]; cha = tolower(cha); someWord[i] = cha; } } // Play one game void play( char *theWord ) { int i; int len = strlen(theWord); int guess = INCORRECT_GUESSES; int result = 0; char soFar[MAXWORD]; fill_array( soFar, len, '*'); printf( "%c", soFar ); for( i=0; i&lt;INCORRECT_GUESSES; i++ ) { get_letter( theWord, soFar ); if( get_letter == 0) { printf( "Sorry, you're out of guesses" ); } else { printf( "You win"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload