Thread: hangman in c issue with while loop and other stuff

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    3

    hangman in c issue with while loop and other stuff

    what am i missing in the game function and is my guessed array setup is right or do i need to put *guessed. also why is it not stopping after length of word in the while loop. also it loops for ever and when you get it right the first time then all letters becomes right after.

    Code:
    
    
    Code:
     #include <stdio.h>
        #include <conio.h>
        #include <string.h>
        
        
        //function prototypes
        
    void game(char [], char [], int);
         int main()
    {
         char word[20] = {'d', 'u','c','k'};
         char guessed[20] ={'*s', '*s','*s','*s' };
         int length = strlen(word);
         
        
         game(word,guessed, length);   
            
         
            
         getch();
         return 0;   
    } 
    void game(char answer[], char guess[], int length)
    {
         
      int life = 5;   
      int x = 0;
      int y = 0;
      char letter;
      int hit = 0;
     while (x < length && life > 0){ 
       
      printf("enter letter\n");
      scanf(" %c",&letter);   
    for (y = 0; y < length; ++y) {
        
        
        if (answer[y] == letter && guess[y] != letter) 
        {
            ++hit;
            guess[y] = letter;
        }
    }
       if (!hit) 
       {
           x += hit;     
           printf("try again\n");
           
           life = life - 1;
           printf("%d tries remaining \n", life); 
       }
       else {
            printf("keep going\n");
            }                          
    
    
                } 
    }



  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Your indentation is very bad - It is rude to ask people for help with bad indentation
    Indent style - Wikipedia, the free encyclopedia

    These are the warnings when I compile your code
    ||=== Build: Debug in codeTesting (compiler: GNU GCC Compiler) ===|
    C:\Programming\codeTesting\main.c||In function 'main':|
    C:\Programming\codeTesting\main.c|12|warning: multi-character character constant [-Wmultichar]|
    C:\Programming\codeTesting\main.c|12|warning: overflow in implicit constant conversion [-Woverflow]|
    C:\Programming\codeTesting\main.c|12|warning: multi-character character constant [-Wmultichar]|
    C:\Programming\codeTesting\main.c|12|warning: overflow in implicit constant conversion [-Woverflow]|
    C:\Programming\codeTesting\main.c|12|warning: multi-character character constant [-Wmultichar]|
    C:\Programming\codeTesting\main.c|12|warning: overflow in implicit constant conversion [-Woverflow]|
    C:\Programming\codeTesting\main.c|12|warning: multi-character character constant [-Wmultichar]|
    C:\Programming\codeTesting\main.c|12|warning: overflow in implicit constant conversion [-Woverflow]|
    ||=== Build finished: 0 error(s), 8 warning(s) (0 minute(s), 3 second(s)) ===|

    What are you trying to do here:
    Code:
        char guessed[20] = {'*s', '*s','*s','*s' };
    You are trying to put 2 chars in one.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, your variable "hit" doesn't do what you want it to.

    Consider what happens when the user enters 'd' on their first turn, and then 'q' on their second turn -> What is the value in "hit"? What does the code do when "hit" is not zero?
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop Issue
    By M_A_T_T in forum C Programming
    Replies: 11
    Last Post: 02-19-2013, 07:29 PM
  2. for loop issue
    By begginer in forum C Programming
    Replies: 8
    Last Post: 03-20-2011, 08:22 AM
  3. Help with for loop stuff
    By belkins in forum C Programming
    Replies: 1
    Last Post: 10-27-2008, 11:38 PM
  4. Loop issue
    By Gordon in forum Windows Programming
    Replies: 14
    Last Post: 01-07-2008, 02:04 AM
  5. nested loop stuff
    By bob20 in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2001, 12:24 PM

Tags for this Thread