Thread: Assignment Help (II)

  1. #16
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Ok the new slightly less mistake-filled function..

    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);
                }
                
                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+1)
            {
                display[i] = 'i';
                
                printf("%s", display);
            }
            return 0;   
        }
        startup = 1;
        
    }
    I added the return 0 at the other outcome.. but it still says it is non-void..

  2. #17
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're still missing the fact that you haven't declared 'startup' as a static variable. Now the value is uninitialised every time the function is called. The compiler should give you a warning about using an uninitialised variable. Look up the static keyword.

    Also, you still are mistakenly using assignment instead of comparison. I.e. your line 15 sets startup to the value of one, it does not test the variable to see if its value is 1.

    And finally, you're still not returning a value at the end of the function.

    So basically, it appears that you haven't understood anything I've said. These are all very important things and your program is very very broken until they are fixed. If you don't understand the advice, please ask questions about it.

    Edit: Okay you posted while I wrote that. You've tried to add another return statement, but you've put it in a bit of a silly place. It can still fall off the end of the function without returning a value. When do you expect it to reach the startup=1; line?
    Last edited by iMalc; 11-24-2012 at 10:08 PM.
    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"

  3. #18
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Well I did ask about static variables earlier but no response.. But I added it in now, thanks. Now you said that each possible result needs there to be a return value right? The only return value I am going to end up using in main is when the guess is false. So what happens with the other values?

    Edit: After further consideration I think I can probably find another way to keep track of when it is wrong and just make the function void..

    Edit 2: Here it is in the entire function. I am aware that the "wrong" bit is very likely done incorrectly though, im trying to figure that out.

    Code:
    #include <stdio.h>#include <string.h>
    
    
    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, wrong;
        static int startup = 0;
        char display[7];
        
        //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);
                }
                
                else wrong = 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+1)
            {
                display[i] = 'i';
                
                printf("%s", display);
            }   
        }
        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
    	
    	    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;
    }
    The overall program still crashes however!
    Last edited by ADH94; 11-24-2012 at 10:38 PM.

  4. #19
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    iMalc has mentioned this line to you at least twice

    Code:
    if((startup = 1))
    That needs to be an ==(comparison) not an =(assignment).

    Code:
    scanf("%c", guess);
    Scanf requires the address of the variable not the value of it. You are missing the '&'

    Take into consideration
    Code:
    scanf(" %c", &guess);
    Notice the leading space before the %, this will allow scanf to ignore any leading white space, which you will inevitably encounter when entering in multiple single chars (this is due to the '\n' being entered in each time you make a guess).
    Last edited by camel-man; 11-24-2012 at 11:12 PM.

  5. #20
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Crap I was focusing on the other things he said to me.. Totally missed that. And I fixed the scanf bit too, thanks.

    I'm going to try and resolve a few little problems right now and then I'll pick it up tomorrow (with more questions being a very likely possibility.)

    Thanks for putting up with my horrendous mistakes!

  6. #21
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Ok so I have the initial dash print thing working properly and rearranged it to be at the top of the function so it actually looks like it should be there (purely for my own preference). Now I am trying to get the guesses to work, but any letter guessed pops up as being incorrect, and the string that should contain the incorrect guesses spouts nonsense out.

    The incorrect guess string I was told how to do rather hastily as the TA was running out of time for the lab, so there are bound to be mistakes in there, but unfortunately I don't know what.

    Here is the newest code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    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, wrong;
        static int startup = 0;
        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    {   
            if(word[i] == guess)
            { 
                for(i=0; i<length; i=i+1)
                {
                    display[i] = guess;
                }
                printf("%s\n", display);
            }
            else wrong = 1; //if an incorrect letter is guessed, wrong keeps track of it
        }
        
        startup = 1; //changes startup so that it can skip the first loop
    }
       
    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
        
            wrong = wrong + 1; //adds to the incorrect guess count
        
            prevguess[position] = guess;//adds the guess into the string
                    
            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;
    }
    Edit: Fixed some problems in the second for loop, updated the code above to reflect that.
    Last edited by ADH94; 11-25-2012 at 12:37 PM.

  7. #22
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Well that is entirely useless. Anyone else?

  8. #23
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    It's spam - Don't forget to report anything you see like this by clicking the triangle in the bottom left with the explanation mark in it.

    [edit] For all those playing at home, the spam has been removed by mods [/edit]
    Last edited by Click_here; 11-25-2012 at 09:11 PM.
    Fact - Beethoven wrote his first symphony in C

  9. #24
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Oh - And NEVER follow any links in posts like this.

    They could be dangerous for you computer.
    Fact - Beethoven wrote his first symphony in C

  10. #25
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Yeah thats what I did, worry not!

    But if anyone can offer any assistance to me, it is still needed and greatly appreciated!

  11. #26
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I haven't been following your posts - But are these two lines back the front?

    Code:
    if(word[i] == guess)
            { 
                for(i=0; i<length; i=i+1)
    Fact - Beethoven wrote his first symphony in C

  12. #27
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Back the front? I'm afraid I dont understand.

  13. #28
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Did you mean to have this?
    Code:
    for(i=0; i<length; i=i+1)
            { 
                if(word[i] == guess)
    Fact - Beethoven wrote his first symphony in C

  14. #29
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, you need "display" to be static - Or something that exists in the scope of "main" -> I'd take the second, because then I can have another function worry about the printing out to the screen.
    Fact - Beethoven wrote his first symphony in C

  15. #30
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    In regards to those first lines, it definitely seems to be backwards now, but now im not sure how to throw in the "else wrong" bit.

    And I slapped a static on the display just to try it out, and now it actually updates the ----- (for hello) to h---- and all that, which is good (I just gotta fix a problem with it printing it over and over) <-EDIT: fixed, im an idiot and included the printf in the loop.

    But can you elaborate on something that exists in the scope of main for me?

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