Thread: Hangman again

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    Question Hangman again

    I'm new around here so I hope I'm asking this properly.I know you guys are probably as sick of Hangman as I am by now, but if you could give me a little advice I would greatly appreciate it. I know this program needs a lot of help, as I'm very new to C programming. This would obviously work better with functions, but I'm just trying to get the program run through before I try writing them. I think I can figure most of the problems out myself but one thing in particular isn't working and I can't figure out why. When the loop that compares the word chosen to the letter input finishes, I can't get the program to reprint the template for the underscores and letters. Correction, I can get it to SOMETIMES print the underscores and letters, but there seems to be no rhyme or reason to it. Or if there is, I am clearly to dim to see it. I can't tell if it's a problem with array sizes, strings, relational operators, or if my computer is expressing deep seated hostility. Any hints?

    P.S.

    If it makes any difference, I'm compiling this under Linux using gcc.


    #include <stdio.h>
    #include <stdlib.h>


    int main ()

    {

    char words[][9]={"strong","quaint","garage","apples","budget","an swer","matter","dollar","pursue","please"};
    char myword[6];
    char nexttime[] = "Maybe next time!";
    char template[6]="______";
    char letter;
    int i, j, k, play;

    srand(time(NULL));

    printf("Would you like to play a game of hangman?\n");
    printf("1 - Yes\n2 - No\n");
    printf("Choose:");
    scanf("%d", &play);


    switch(play){

    case 2:

    printf("%s\n", nexttime);
    break;

    case 1:#include <stdio.h>
    #include <stdlib.h>


    int main ()

    {

    char words[][9]={"strong","quaint","garage","apples","budget","an swer","matter","dollar","pursue","please"};
    char myword[6];
    char nexttime[] = "Maybe next time!";
    char template[6]="______";
    char letter;
    int i, j, k, play;

    srand(time(NULL));

    printf("Would you like to play a game of hangman?\n");
    printf("1 - Yes\n2 - No\n");
    printf("Choose:");
    scanf("%d", &play);


    switch(play){

    case 2:

    printf("%s\n", nexttime);
    break;

    case 1:

    i=1+(rand()%9);
    strcpy(myword, words[i]);
    printf("\n");
    printf(template);


    for(j=0; j<=5; j++){

    printf("\nGuess a letter:");
    scanf("%s", &letter);




    for(k=0; k<=5; k++){



    if(myword[k] == letter)
    template[k]=letter;

    }


    printf(template); /* I just stuck this here for now, because it doesn't work properly anywhere. */
    }




    break;

    }


    return 0;

    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It seems to work fine once I changed

    char template[6]="______";

    to

    char template[7] = "______";

    Don't forget that a string literal includes N characters plus 1, the nul terminator. As it was your template array had no nul and was causing problems. You also neglect to include string.h and time.h even though you use strcpy and time in your program. The words array has a similar problem, it should be [][10], not [][9]. There are further ways you can improve, but since this is a test run I'll refrain from pointing them all out.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    Thank you.

    Thank you very much for helping me out. Looking at my post again, I can see that my code got a little screwed up during the copy&paste process. What can I say, copy& paste under Linux is still not..um...perfect. Anyway, I followed your suggestions(which of course made perfect sense), and while they have obviously fixed major issues, they still do not enable the printing of the template after each letter guessed, no matter where I stick the printf(template); or how many prayers I say before I execute the program. ARGHHH!

    Sorry, I needed to vent.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    Update

    I just wanted to thank you again, and let you know that after compiling the program with Visual C/C++ it worked. I don't know why it didn't work using gcc, but at least I know it does work.

    Thanks!

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This bit also has a bug:

    >char myword[6];
    >strcpy(myword, words[i]);
    Some of the words in the words[] array are 6 characters long, therefore you have exceed the myword buffer length.

    If you find that your code appears to work on one system, but not on another, I would normally expect that to still be a bug in your code. It's just manifesting itself differently.

    This is possibly due to the way the variables are stored in memory. For example, when you exceed the buffer length of a particular variable, you might get a problem if the data you overwrite in error is critical to your program. Here's an attempt to show you what I mean:
    Code:
    char a[4] = "A";
    char b[4] = "B";
    
    In memory, a and b MIGHT follow each other.
    
    Address Offset:  01230123
    Contents:        A   B
    
    When you do this
    strcpy(a, "Hammer");
    the memory now looks like this:
    
    Address Offset:  01230123
    Contents:        Hammer
    
    As you can see, the data in b has been replaced in error.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    red baron has coded a very nice version of hangman
    just have a look at it to see what you can do with plain c

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    Smile New and improved

    Thanks!

    After following your suggestions (I *thought* there was a problem with the size of the myword array), this is what I finally came up with, and it works!


    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>




    int main (loop)

    {


    char words[][10]={"strong","quaint","garage","apples","budget","an swer","matter","dollar","pursue","please"};
    char myword[7];
    char nexttime[] = "Maybe next time!";
    char template[7]="______";
    char guess[7];
    char letter, yn;
    int i, j, k, play;


    srand(time(NULL));

    printf("Would you like to play a game of hangman?\n");
    printf("1 - Yes\n2 - No\n");
    printf("Choose:");
    scanf("%d", &play);


    switch(play){

    case 1:

    system("cls");
    i=1+(rand()%9);
    strcpy(myword, words[i]);
    printf(template);
    printf("\n");



    for(j=0; j<=7; j++){

    printf("\nGuess a letter: ");
    scanf("%s", &letter);
    system("cls");



    for(k=0; k<=5; k++){


    if(myword[k] == letter)
    template[k]=letter;

    if(strcmp(myword, template)==0) {
    printf("You win!\n");
    printf("\n");
    return main (loop);

    }


    }

    printf(template);
    printf("\n");


    printf("\nWould you like to guess the 6 letter word? y or n: ");
    scanf("%s", &yn);

    switch(yn){

    case 'y':

    printf("Word:");
    scanf("%s", guess);


    if(strcmp(myword, guess)==0) {
    printf("You win!\n");
    printf("\n");
    return main (loop);

    }
    else {
    printf("Wrong! ");
    }




    break;


    case 'n':

    system("cls");
    printf(template);
    printf("\n");

    break;


    }




    }





    break;


    case 2:

    printf("%s\n", nexttime);

    break;


    default:

    printf("\nYou must choose option 1 or option 2!\n");
    printf("\n");
    return main (loop);


    }






    return 0;

    }

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What, prey tell, is this supposed to be:
    >int main (loop)
    and what is this:
    >return(main(loop));

    If you're trying to do recursive calls to main(), don't. Only the OS invokes main(), not your own code!

    And please use code tags when posting code!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    lol dune you keep on telling ppl i have a good example of hangman,etc. on my website and you never tell ppl where it is

    just follow the link in my sig the hangman is ok but is 2 player and is pretty much ok, but i think i used like 1 goto statement i'm not sure, have fun
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  10. #10
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    @red baron
    yeah i just can't keep your url in mind...
    so that's why i am just telling them your name

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    I know goto is bad.

    I was just trying to get it to run through once. It looks like this now:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #include "game.h"
    
    #define  TEMPLATE "______"
    
    
    int main ()
    {
    
    
    	char words[][10]={"strong","quaint","garage","apples","budget","answer","matter","dollar","pursue","please"};
    	char myword[7];
    	char nexttime[] = "Maybe next time!";
    	char template[7];
    	char guess[7];
    	char used[] = "        "; 
    	char letter, yn;
    	int i, j, k, play, finish;
    
    
    	srand(time(NULL));
    
    	play = menu();
    
        while( play == 1)
    	{
    		switch(play)
    		{
    
    			case 1:
                    strcpy(template,TEMPLATE);
    				strcpy(used,"        ");
                    system("cls");
    				i=1+(rand()%9);
    				strcpy(myword, words[i]);
    				printf("\n");
    				printf(template);
    				printf("\n");
    				printf("\n");
                    printf("\n");
    				
    				finish = 0;
    				for(j=0; j<=7 && !finish; j++)
    				{
    
                        printf("\nPlease enter a letter: ");
    				    scanf("%s", &letter);
    					used[j]=letter;
    					system("cls");
    							
                                 
    						      
    					for(k=0; k<=5 && !finish; k++)
    					{
    
         					 if(myword[k] == letter)
    							 template[k]=letter;
    									 
    						 if(strcmp(myword, template)==0) 
    						 {
    							 printf("\nCorrect, the word was %s.\n", myword);
    							 printf("\n");
    							 finish = 1;
    															 
    						}
    												
    					}
    
    					printf("\n");
    					printf(template);
    					printf("\n");
                        printf("\n");
                        printf("Letters guessed: %s", used);
    					printf("\n");
                        if (!finish)
    					{
    					     printf("\nWould you like to guess the 6 letter word? y or n: ");
    						 scanf("%s", &yn);
    						 switch(yn)
    						 {
    
    							case 'y':
    								printf("Word:");
    								scanf("%s", guess);
    								if(strcmp(myword, guess)==0) 
    								{
    									 printf("\nCorrect, the word was %s.\n", myword);
    									 printf("\n");
    									 finish = 1;
    															 
    								}
    								else 
    								{
    								    printf("\nWrong!\n ");
    								}
      								break;
    											
    							case 'n': 
    
    								system("cls");
    								printf("\n");
    								printf(template);
    								printf("\n"); 
                                    printf("\n");
    								printf("Letters guessed: %s", used);
    								printf("\n");
    
    							    break;
    						 }
    					}
    				}				   
    				
            		if (!finish) 
                    {
    					printf("\nYou lost! \n");
    				}
    				break;
    		    
    
    
    		   case 2:
    
    				printf("%s\n", nexttime);
    
    			break;
    
    			
    		   default:
    
    			   printf("\nYou must choose option 1 or option 2!\n");
    			   printf("\n");
    		}
    		play = menu();
    	}
    
         
    
    
    				
    		   
     return 0;
    
    }

    p.s.

    red_baron, I found your games page and I have to tell you, my boyfriend loved your Moria game. He played with it for like an hour and a half last night.
    Last edited by Kat; 07-16-2002 at 10:44 AM.

  12. #12
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    Use ]code[ ]/code[ (invert the brackets) next time
    .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman Help - Code included
    By darren78 in forum C Programming
    Replies: 3
    Last Post: 02-17-2009, 09:35 AM
  2. Hangman Game Troubles
    By emerica240 in forum C Programming
    Replies: 9
    Last Post: 11-26-2008, 01:39 AM
  3. Hangman, Help me with the thinking
    By Livijn in forum C# Programming
    Replies: 14
    Last Post: 02-09-2008, 03:16 PM
  4. Help doing Hangman...
    By Kreative in forum C Programming
    Replies: 11
    Last Post: 08-18-2002, 09:22 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM