Thread: Hangman-esque

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    8

    Hangman-esque

    Hi, I've been having some trouble with a "hangman-esque" program. I have it so that a person can enter a letter, and make it show up in the right spot, but I can't get the program to ask again, until the phrase has been completed. I've tried a do/while loop, but that hasn't done it. I would appreciate any help or suggestions you can make. Also, once I get the program to re-ask, how would I mange to keep the already guessed charecters in the correct spot? Thanks.

    Code:
    #include <stdio.h>
    main()
    {
            char text[40]="EVEN BETTER HARRY POTTER";
    /* Phrase not of my origin */        
            printf("Let's play Wheel-of-Fortune\n");
            char ch_in;
            int kk, len, found;
            len = (int)strlen(text);
            printf("There are %d charecters\n",len);
            printf("Enter an upper-case charecter:");
            ch_in = getchar();
            printf("%c was entered.\n", ch_in);
            found = 0;
            for(kk=0; kk<len; kk++){
             if(text[kk] == ch_in){
                   found = 1;
                   printf("%c", ch_in);
             }else printf(".");        
            }
            printf("\n");
            if(found==0) printf("No Match!\n");
    }
    Last edited by Inexorable; 12-11-2002 at 01:41 AM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Your code seems to be filled with errors. First off, your while loop is pretty pointless. There are a couple other errors (such as your for loop) but I'd start with the while loop.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    8
    The while loop was just something to try. As for the for loop, that is how the entered character was checked against the charecter string. If there is anyother way to check if the entered character is correct, I'm all ears.
    Last edited by Inexorable; 12-11-2002 at 01:51 AM.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Fair enough, but in the for loop you have this:

    Code:
             if(text[kk] == ch_in){
                   found = 1;
                   printf("%c", ch_in);
             }else printf(".");
    Unless printf(".") is just for debugging purposes then you are going to have a big mess.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    8
    Hmm. Well, the printf("."); is left over from a prior incarnation of the program where I just had to show where an inputed character was in the phrase, and nothing further. The .s were there to act as placeholders.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    On to big issue #2. How do you keep track of which letter you know and which you don't?

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    8
    That's the crux of the matter. I am trying to keep already entered and correct characters visible throughout the the run of the program, until they guess an incorrcet letter, or correctly fill out the phrase.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Just have an array that is the same size as the string. Zero out that array. Now you would place ones in the places corresponding with the character that a player has found.

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    8
    Okay, I think I understand what you're saying. However, will that make the program continue to ask for another input until they guess wrong or complete it?

    And, could you possibly show an example of that specific use of an array? Thanks.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Sure:

    Example

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void) {
    	char text[40]="EVEN BETTER HARRY POTTER", *mask;
    	char ch_in;
    	int kk, len, found, done;
    
    	/* Phrase not of my origin */
    
    	printf("Let's play Wheel-of-Fortune\n");
    	len = (int)strlen(text);
    	mask = (char *)malloc(len);
    	memset(mask, 0, len);
    
    
    	for(kk = 0; kk < len; kk++)
    		if(text[kk] != ' ')
    			printf("_ ");
    		else {
    			mask[kk] = 1;
    			printf("  ");
    		}
    	putc('\n', stdout);
    
    	done = 0;
    
    	do {
    		found = 0;
    		done = 1;
    
    		printf("Enter a letter: ");
    		ch_in = toupper(getchar());
    
    		// this is a nasty way of handling the newline that you get from
    		// getchar()
    		if(ch_in == '\n') {
    			putc('\r', stdout);
    			done = 0;
    			continue;
    		}
    
    		if(isalpha(ch_in)) {
    			for(kk=0; kk<len; kk++){
    				if((toupper(text[kk]) == ch_in) && (text[kk] != ' ')){
    					found = 1;
    					mask[kk] = 1;
    					putc(ch_in, stdout);
    				} else {
    					if(mask[kk])
    						putc(text[kk], stdout);
    					else {
    						if(text[kk] != ' ')
    							printf("_ ");
    	  					else
    	  						printf("  ");
    						done = 0; // if a letter is missing then we aren't done yet
    					}
    				}
    			}
    			putc('\n', stdout);
    
    			if(!found)
    				puts("No Match!");
    		} else
    			puts("Invalid character!");
    
    		printf("%d %d\n", found, done);
    
    	} while(!done);
    
    	puts("You win!");
    	free(mask);
    
    	return 0;
    
    }
    Last edited by master5001; 12-11-2002 at 03:51 AM.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    8
    Thanks Master, you have extremely helpful.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    No problem.

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