Note that there are some explanatory texts on larger screens.

plurals
  1. POHangman Game using C issues
    primarykey
    data
    text
    <p>Newbie to C language and I'm trying to make a hangman game. I have the following code:</p> <pre><code>int main(void) { char userResponse;//This is the response from the user on rather they want to continue or not. do { startGame(); printf("Do you want to continue (y or n): "); scanf("%c", &amp;userResponse); } while(userResponse == 'y'); return 0; } int startGame(void) { int num_letters = 0; //length of word array int count = 0; int tries = 0; //total tries player has used int correctGuesses = 0; //number of correct guesses int correctLetter = 0; //flag to check if the letter guessed was correct. int repeatLetter = 0; //flag to check if the letter guessed was already guessed before. int randomWord = 1+(rand()%6); //setting a variable that will pick a word from the word array at random. char word[] = {"spot", "physicist", "penny", "klingon", "bazinga", "continuum"}; // should these be all lower char or does it matter? char guess; char incorrectGuesses[5] = " "; // all the incorrect letters chosen by the player will be stored in this array. char gameWord[num_letters]; //starts to fill out the word as the player guesses correctly srand(time(NULL)); // Don't really understand this line but the interweb said I needed it. Can you tell me why? num_letters = strlen(word); gameWord[num_letters] = '\0'; //This will show the player what letter they have typed incorrectly. for(count = 0; count &lt; num_letters; count++) gameWord[count] = '-'; // This will display the length of the word represented with a "-". printf("Welcome to Big Bang Hangman!\n\n"); printf("Time to guess the word!"); draw_hangman(tries); while(tries &lt; NUM_TRIES_ALLOWED) { //Player only gets 7 guesses. Found this on the interweb. printf("Here is your word: %s\n", gameWord); printf("Incorrect Letters: %s\n", incorrectGuesses); printf("\nGuess a letter (and press 'Enter'): "); fgets(&amp;guess, sizeof(guess), stdin); //This compares the current guess with the previous guesses so that you won't affect a turn if you pick the same incorrect letter. for(count = 0; count &lt; num_letters; count++) if(guess == gameWord[count] || guess == incorrectGuesses[count]) { // If the player guess is equal to the size of the word or if its equal to the repeatLetter = 1; correctLetter = 1; break; } if(repeatLetter == 0) // setting the flag back to 0 tells the compiler the game is over. //Checks for matches in string for(count = 0; count &lt; num_letters; count++) { if(guess == word[count]) { // can't understand why I'm getting this warrning. gameWord[count] = guess; correctGuesses++; //if (correctGuesses == count) { //printf("\n\nCONGRATULATIONS! You guessed the word %s!", gameWord); //exit(0); //} if(correctGuesses == num_letters) { printf("\n\nCONGRATULATIONS! You guessed the word %s!", gameWord); exit(0); } correctLetter = 1; } } if(correctLetter == 0) { incorrectGuesses[tries] = guess; tries++; } // reset both flags repeatLetter = 0; correctLetter = 0; //startGame(); // Start the game over when the flag is reset. } printf("Sorry but you did not guess the word.\n"); printf("The word is: %s\n\n", gameWord); return 0; } </code></pre> <p>It is outputting the following:</p> <p>Welcome to Big Bang Hangman!</p> <p>Time to guess the word!</p> <pre><code> ___ | | | | | --- </code></pre> <pre> Here is your word: ---- Incorrect Letters: Guess a letter (and press 'Enter'): Here is your word: ---- Incorrect Letters: </pre> <p>The last two lines continue on forever until I stop it. I'm completely lost.<br> Any advice would be great. </p>
    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.
 

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