Thread: Assignment Help (II)

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    29

    Assignment Help (II)

    Hi, a while ago I posted here for help on an assignment for school, and the help I received was invaluable and helped me figure out how to complete my assignment!

    This is my final assignment now, and it is programming a game of Hangman. It requires a function in addition to main, and uses strings.

    I think I have the majority done, the rest is just finding and correcting mistakes (along with a few other questions that will likely arise later)..

    I'm not very good with strings, so there may be some pretty stupid errors..

    And for some reason it crashes after inputting the word... My compiler (and I) must be missing something..

    Here's my code:

    Code:
    #include <stdio.h>#include <string.h>
    
    
    int draw_guess(int length, char guess, char word[]) //the function draws the unguessed letters (dashes) and updates it when guessed correctly
    {
        //Draws the dashes
        int i, startup = 0;
        char display[i];
        
        
        //Updates the dashes with the properly guessed letters 
        if(startup = 1)//only goes after the initial dashes are printed
            for(i=0; i<length; i++)
            {
                if(word[i] == guess)
                {
                    display[i] = guess;
                    
                    printf("%s", display[i]);
                }
            
                else return 1; //if an incorrect letter is guessed, the function returns 1
            }
        else//this is just to print the initial dashes
        {
            for(i=0; i<length; i++)
            {
                display[i] = 'i';
                
                printf("%s", display[i]);
            }
        }
        startup = 1;
        
    }
       
    
    
    int main(void)
    {
        int length, wrong, position;
        char guess;
        char word[7], prevguess[17];
        
        wrong = 0;
        
        printf("Welcome to HANGMAN!\n");
        printf("Please pick a word (2 to 6 letters): ");
        scanf("%s", word);
        
        length = strlen(word); //just takes the length of the word entered
        
        printf("You are guessing a word that is %d letters long.\n", length);
        
        while(wrong != 10) //keeps going for 10 incorrect guesses
        {
            draw_guess(length, guess, word); //SHOULD draw the initial dashes once, than afterwards begin printing the updated ones
            
            printf("Make a guess (1 letter): ");
            scanf("%c", guess);
        
            position = strlen(prevguess); //this just sets up the string containing guesses
        
        if(draw_guess(length, guess, word) == 1)//when the function returns 1, the guess was wrong
            wrong = wrong + 1; //adds to the incorrect guess count
        
            prevguess[position] = guess;//adds the guess into the string
                    
            printf("Your guesses: %s", prevguess);//shows the previous guesses
            printf("Incorrect guesses: %d", wrong);//tells user how many times they have guessed wrong
        }
        
        return 0;
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Just one thing I noticed first was this piece of code.

    Code:
     int i, startup = 0;
        char display[i];
    what is the value of i?

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Line 12 is not doing what you think it is, you've mistaken asignment for comparison.

    However, even if it was correct, why would you ever expect it to be equal to one when you've just assigned zero to it? Oh I see, you're treating the variable as if it were declared as static, except that you haven't declared it as static.

    You should also turn your compiler warnings on and pay attention to them. A function whose return type is non-void, must return something in every case. Right now you have it just falling off the end.
    Last edited by iMalc; 11-24-2012 at 12:14 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    What are you trying to accomplish ? Can you explain ?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by justine View Post
    What are you trying to accomplish ? Can you explain ?
    In the game of Hangman, the display will show the word being guessed, with the letters not guessed correctly yet, replaced by either underlines or hyphens. Letters which have been guessed, are displayed normally. This is in addition to the man in peril of being hanged, of course.

    If the correct word was "display" and you had guessed the 'a' correctly, it would be shown as: _ _ _ _ _ a _ or - - - - - a -, with an extra space between the letters.

    You need a word list in a file, with a selection of common words, and varying length. Two letter words are very rare in English - I'd make the minimum number of letters at least three.

    0 to 10 guesses is a total of 11 guesses. So make while(wrong < 10) for 10 wrong guesses.
    Last edited by Adak; 11-24-2012 at 03:47 AM.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    The whole thing is still unclear to me. What is a wordlist ?
    I just created a sample code :
    Code:
    #include <stdio.h>
    #include <string.h>
     
    int draw_guess(int length, char guess, char word[]) 
    {
        
        int i, k=0;
        for(i=0; i<length; i++)
        {
            if(word[i] == guess)
            k=1;
        }
        if(k==1)
        {
            for(i=0; i<length; i++)
            {
                if(word[i] == guess)
                printf("%c", guess);
                else if (word[i]!=guess)
                printf("%c", '-');
            }  
            return 0;
        }  
        else if (k==0)
        return 1;
    }
     
    int main(void)
    {
        int length, wrong, j;
        char guess;
        char word[20];     
        wrong = 0;     
        printf("Welcome to HANGMAN!\n");
        printf("Please pick a word (2 to 6 letters): ");
        scanf("%s", word);     
        length = strlen(word);
        printf("You are guessing a word that is %d letters long.\n", length);
        while (wrong<=9)
        {
             printf("Make a guess (1 letter): ");
             scanf(" %c", &guess);
             draw_guess(length, guess, word);
             j=draw_guess(length, guess, word);     
             if(j==1)
             wrong++;
             else if(j==0)
             wrong=100;
        }
        return 0;
    }
    It displays two time the result if guess is true. But it may not be the actual program which is not clear to me.
    Why it display two time the result ?
    Thanks.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Quote Originally Posted by camel-man View Post
    Just one thing I noticed first was this piece of code.

    Code:
     int i, startup = 0;
        char display[i];
    what is the value of i?
    Our TA's showed us this exactly. The value of i is in the for loop im pretty sure.

    Quote Originally Posted by iMalc View Post
    Line 12 is not doing what you think it is, you've mistaken asignment for comparison.

    However, even if it was correct, why would you ever expect it to be equal to one when you've just assigned zero to it? Oh I see, you're treating the variable as if it were declared as static, except that you haven't declared it as static.

    You should also turn your compiler warnings on and pay attention to them. A function whose return type is non-void, must return something in every case. Right now you have it just falling off the end.
    Yeah I just realized that.. So the variable resets each time the function is used? I added that last bit in hastily and didnt really think about it. Aaaand I totally forgot about the warnings

    Quote Originally Posted by Adak View Post
    You need a word list in a file, with a selection of common words, and varying length.
    This game is supposed to be two player, so even though the other player can totally see what you've inputted as the word, it doesnt really matter.. Its just the assignment. So no word bank, the word itself is picked by a player.
    Last edited by ADH94; 11-24-2012 at 10:34 AM.

  8. #8
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Our TA's showed us this exactly. The value of i is in the for loop im pretty sure.
    The for loop comes after your declaration of your array, so how could it possibly know the value of i? Either your TA's made a mistake or they are testing you and waiting on you to catch it on your own.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Quote Originally Posted by camel-man View Post
    The for loop comes after your declaration of your array, so how could it possibly know the value of i? Either your TA's made a mistake or they are testing you and waiting on you to catch it on your own.
    They demonstrated and it worked fine.. Unless it should be declared as dashes[7] (since there can only be 6 dashes maximum) and my mind conveniently swapped that out with "i"

  10. #10
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Quote Originally Posted by ADH94 View Post
    .. Unless it should be declared as dashes[7] (since there can only be 6 dashes maximum) and my mind conveniently swapped that out with "i"

    That seems more likely.

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Ok so I've done some tinkering and most of the issues lie in the draw function. Here's what i've done.. sorry if it offends your expert eyes haha

    Code:
    #include <stdio.h>#include <string.h>
    
    
    int draw_guess(int length, char guess, char word[]) //the function draws the unguessed letters (dashes) and updates it when guessed correctly
    {
        //Draws the dashes
        int i, startup;
        char display[7];
        
        if(startup != 1)
            startup = 0;
    
    
        //Updates the dashes with the properly guessed letters 
        if((startup = 1))//only goes after the initial dashes are printed
        {   
            for(i=0; i<length; i++)
            {
                if(word[i] == guess)
                {
                    display[i] = guess;
                    
                    printf("%s", display[i]);
                }
                
                else return 1; //if an incorrect letter is guessed, the function returns 1
        }
    
    
        else//this is just to print the initial dashes
        {
            for(i=0; i<length; i=i+)
            {
                display[i] = 'i';
                
                printf("%s", display[i]);
            }
        }
        startup = 1;
        
    }
    Since each possibility needs a return value, but I only care about when it is false, would just assigning a different return value change what the "correct guess" part of the assignment displays?

    My compiler is giving some warnings that I dont quite understand either..

    A syntax error in line 30 before the ) token

    both printf("%s blah blah) parts come up saying argument is not a pointer

    line 37 type defaults to "int" in declaration of startup
    Last edited by ADH94; 11-24-2012 at 09:10 PM.

  12. #12
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    for your printf Errors
    Code:
    printf("%s", display[i])
    display[i] is a character, you either need to change %s to %c or change display[i] to display.

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    I need it to be a string though..

  14. #14
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Then change
    Code:
    printf("%s", display[i])
    to
    Code:
     printf("%s", display)

  15. #15
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Gah i totally knew that! I keep making such stupid mistakes.. Thanks for that one

    I also just fixed a bunch of little errors like a missing } and forgetting a number... so dont worry about those guys in the code, ill post an updated one once I get the last few guys fixed.
    Last edited by ADH94; 11-24-2012 at 09:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment help
    By Rastafarian in forum C# Programming
    Replies: 3
    Last Post: 01-09-2011, 07:33 AM
  2. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  3. First assignment help
    By FirstC++ in forum C++ Programming
    Replies: 7
    Last Post: 07-01-2004, 07:57 AM
  4. Help With Assignment Please
    By MegaVortex in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2004, 09:40 PM
  5. course assignment
    By kesam in forum C Programming
    Replies: 1
    Last Post: 01-06-2003, 01:51 PM