Thread: Assignment Help (II)

  1. #31
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    If the guessing string is declared next to the variable "word" it would have that scope.

    The advantage to that is that you could update it in "draw_guess" and then print it in another function.

    Also, you can return an integer from the function - 1 for found, 0 for not found.
    Fact - Beethoven wrote his first symphony in C

  2. #32
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    I wanted to try something like that initially, but I was getting too snarled up with everything screwing up the rest of the program.

    I just have guess as a character and not a string. Should I change it to
    Code:
    int length, wrong, position;    
    char word[7], guess[7], prevguess[17];
    And then have a new function (something like check_guess) which takes the guess and word and returns a 1 or 0 based on whether the guess is in the word? Then that 1 or 0 can be used for a wrong/right counter?
    Last edited by ADH94; 11-25-2012 at 09:38 PM.

  3. #33
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Should I change it to ...
    It's up to you what you want to do.

    I'd imagine that you would have something like this:
    Code:
    //Renamed check_guess to something more self descriptive of the return value...
    if ( !input_is_correct(actual_word, displayed_word, user_guess, word_length))
    {
      wrong_guesses++;
    }
    
    display_progress(displayed_word, wrong_guesses);
    Fact - Beethoven wrote his first symphony in C

  4. #34
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Sorry to bump, but im really running short on time and I'm SO close to finishing! If anyone can help me out soon, that would be amazing.

    Code so far:
    Code:
    #include <stdio.h>#include <string.h>
    
    
    int check_guess(int length, char guess, char word[])
    {
        int i;
        
        for(i=0; i<length; i=i+1)
        {   
            if(word[i] == guess)
            {    
                return 1;
                break;
            }
            
            else
            {
                return 0;
                break;
            }
        }
    }
    
    
    void 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;
        static int startup = 0;
        static char display[7];
        
        //Updates the dashes with the properly guessed letters 
        if(startup == 0)//only runs the first time
        {   
            for(i=0; i<length; i=i+1)
            {
                display[i] = '-';
            }
            printf("%s\n", display);   
        }
    
    
        else//after the first time this starts printing with correctly guessed letters
        {
            for(i=0; i<length; i=i+1)
            {   if(word[i] == guess)
                {
                    display[i] = guess;
                }
            }
        
            printf("%s\n", display);
        }
        
        startup = 1; //changes startup so that it can skip the first loop
    }
       
    int main(void)
    {
        int length, wrong, correct, position;
        char guess;
        char word[7], prevguess[17];
        
        wrong = 0;
        correct = 0;
        
        printf("Welcome to HANGMAN!\n");
        printf("Please pick a word (3 to 6 letters): ");
        scanf("%s", word);
        
        length = strlen(word); //just takes the length of the word entered
        
        while(length > 6 || length < 3)
        {
            printf("Your word cannot be less than 3 or more than 6 letters\n");
            printf("Please pick a word (3 to 6 letters): ");
            scanf("%s", word);
        }
        
        printf("You are guessing a word that is %d letters long.\n", length);
        
        draw_guess(length, guess, word);//draws initial dashes
        
        while(wrong != 10) //keeps going for 10 incorrect guesses
        {
            if(correct == length)
                break;
            
            if(check_guess(length, guess, word) == 1)        
            {
                draw_guess(length, guess, word); //SHOULD draw the initial dashes once, than afterwards begin printing the updated ones
                correct = correct + 1;
            }
            
            else wrong = wrong + 1;
            
            printf("Make a guess (1 letter): ");
            scanf(" %c", &guess);
    	
            position = strlen(prevguess); //this just sets up the string containing guesses
            prevguess[position] = guess;//adds the guess into the string
            
            wrong = wrong + 1; //adds to the incorrect guess count
                    
            printf("Your guesses: %s\n", prevguess);//shows the previous guesses
            printf("Incorrect guesses: %d\n", wrong);//tells user how many times they have guessed wrong
        }
        
        return 0;
    }
    Now in the first "check_guess" function, I am aware that the return 0 is inside the for loop and therefore will most certainly return a zero (unless the player guesses the last letter). I'm having difficulty figuring out how to run the whole for loop and then return a zero if the guess was not in the string at all.

    Also in the main function I need help figuring out why my "previous guesses" string won't work. I copied this from a TA but they were hasty and I probably forgot something, but I can't figure out what.

    If I could just get help with these spots it would be great,

    Thanks!

    (p.s. I promise this is the last assignment/headache I subject you all to!)

  5. #35
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There's no point having a break after a return statement, and you still have a problem with it potentially running off the end of the function; In this case it happens when length is zero. The compiler should be warning you about that.
    An easy fix for your function might be to replace one of your returns statements with a continue statement, and put the return at the end instead.
    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"

  6. #36
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    length = strlen(word); //just takes the length of the word entered
        
    while(length > 6 || length < 3)
    {
        printf("Your word cannot be less than 3 or more than 6 letters\n");
        printf("Please pick a word (3 to 6 letters): ");
        scanf("%s", word);
    }
    If the user enters an invalid word you have to update "length" inside the loop. Otherwise it won't change and you have an infinite loop.

    Code:
    position = strlen(prevguess); //this just sets up the string containing guesses
    prevguess[position] = guess;//adds the guess into the string
    You don't initialize "prevguess" thus it contains garbage and strlen() returns garbage.
    I don't think you need to calculate the length of "prevguess". Just increment position everytime you add a new character to "prevguess". You also have to terminate "prevguess" with '\0' if you want to print it out as a string.

    Bye, Andreas

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