Hangman Help - Code included

This is a discussion on Hangman Help - Code included within the C Programming forums, part of the General Programming Boards category; Hi, I need help with me Hangman code below please. I have only been programming for 5 weeks so my ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Hangman Help - Code included

    Hi,

    I need help with me Hangman code below please. I have only been programming for 5 weeks so my lecturer isn't concerned with memory efficiency etc, only that the program works. The part that I am stuggling with is when the user enters the letter and then for it to be checked against the array. I can get it to check the array and print out if it finds the letter, however if it doesnt find the letter, i am having problems......if you could please look at my code and let me know....thanks...

    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    
    {
    
    char words[10][10];   /*array to store imported file strings*/
    int buffer;           /*temporarily stores random word from words array*/
    char gameword[10];    /*array to store playable gameword*/
    int i;                /*counter*/
    int option;           /*Menu option*/
    int lives;            /*Number of game lives*/
    char letter;			    /*stores letter guessed*/
    char guessedword[10];   /*all letters will be set to "*" and changed as the user guesses correctly*/
    int counter;
    int number;
    int x;
    
    
    
    FILE *file;
    
    file = fopen("test.txt", "r");
    
    i = 0 ;
    while(!feof(file))
    
    {
    
    	/* loop through and store the numbers into the array */
    	fscanf(file, "%s", &words[i]);
    	i++;
    }
    
    fclose(file);
    
    srand(time(NULL));               /*These 3 lines of code generate a random*/
    buffer=words[(1 + rand() % 4)];  /*word from the imported file into the buffer*/
    strcpy(gameword, buffer);        /*and then copies it into a playable array*/
    number = strlen(gameword);
    
    for ( counter = 0; counter < number; counter++)
    {
    guessedword[counter] = *"*";    /*sets all guessedword characters to "*" */
    }
    
    /*Menu text*/
    
    printf("Hello and Welcome to Hangman in C\n\n");
    printf("Please choose one of the following options: \n\n");
    printf("1. Beginner Difficulty - 9 Lives\n");
    printf("2. Intermediate Difficulty - 6 Lives\n");
    printf("3. Hard Difficulty -  3 Lives\n");
    printf("4. Exit Game\n\n");
    printf("Make your selection:  ");
    scanf("%d", &option);
    
    
    if (option == 1)         /*Determines number of games lives depending upon*/
                             /*user input*/
    {
    lives = 9;
    }
    
    	else if (option == 2)
    	{
    		lives = 6;
    	}
    
    	else if (option == 3)
    	{
    	lives = 3;
    	}
    
    	else if (option == 4)
       {
        return 0;
       }
    
    system ("cls");     		/*Clears screen*/
    
    if (option == 1)        /*Displays message depending upon user input*/
    
    {
    printf("\nSo you've gone for the easy option - What's the matter, feeling chicken?\n\n ");
    }
    
    	else if (option == 2)
    
       {
       printf("So you're a middle of the road type of guy then!\n\n ");
       }
    
       else if (option == 3)
       {
       printf("So you've gone for the Hard difficulty - Good luck, you are going to hang\n\n");
       }
    
    printf("%s\n\n\n", gameword);
    printf("%s", guessedword);
    
    
    printf("Please choose a letter ");
    scanf("%s", &letter);
    
    
    
    
    for (x=0; x<number; x++)
    
    {
    
    if (letter == gameword[x])
    {
    guessedword[x] = letter;
    printf("You have made a correct guess\n\n%s", guessedword);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you need to count how many letters were placed during your for-loop (i.e., at the start you set letters_this_time = 0 and then when you find a letter add one to the count) -- if, at the end of the for loop, you placed 0 letters then it's a miss.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Thanks for the response, but it doesn't seem to work. Is this how you mean:

    Code:
    for (x=0; x<number; x++)
    
    {
    letters_guessed_this_time == 0;
    
    if (letter == gameword[x])
    {
    guessedword[x] = letter;
    printf("You have made a correct guess\n\n%s", guessedword);
    letters_guessed_this_time++;
    }
    
    if (letters_guessed_this_time == 0)
    {
    printf("Incorrect guess");
    
    printf("%d", letters_guessed_this_time);
    
    }
    }
    When I compile that, it prints an error message off for each character in the word. So if I had an 8 letter word, it would print off the error message 8 times as it scans through the string on the for loop. Is there a way to limit the scope of the variable letters_guessed_this_time to only 1 integer irrespective of the length of the word and therefore the for loop?

    Thanks again!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe he had something like this in mind:

    Quote Originally Posted by darren78 View Post
    Thanks for the response, but it doesn't seem to work. Is this how you mean:

    Code:
    for (x=0, letters_guessed_this_time ==0; x<number; x++)
    
    {
    
       if (letter == gameword[x])
       {
          guessedword[x] = letter;
          printf("You have made a correct guess\n\n%s", guessedword);
          letters_guessed_this_time++;
       }
       else
       {
          printf("Incorrect guess");
       }
        printf("%d", letters_guessed_this_time);
    }
    When I compile that, it prints an error message off for each character in the word. So if I had an 8 letter word, it would print off the error message 8 times as it scans through the string on the for loop. Is there a way to limit the scope of the variable letters_guessed_this_time to only 1 integer irrespective of the length of the word and therefore the for loop?

    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 01-10-2007, 06:53 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 01:48 AM
  3. Lining up decimals & text - code included inside.
    By INFERNO2K in forum C Programming
    Replies: 7
    Last Post: 11-27-2004, 04:49 PM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21