I have to create a basic hangman game for a class using two arrays. What I am struggling with is when the user inputs the letters and gets them right i dont know how to output the letters hes has right in the mystery word
I have to create a basic hangman game for a class using two arrays. What I am struggling with is when the user inputs the letters and gets them right i dont know how to output the letters hes has right in the mystery word
I can only offer suggestions based on knowing what you have already coded. So be a good sport and post some code for me, por favor.
This is what i have but it is not anything near correct, i'm just not sure how to go about doing this.Code:#include <stdio.h> #include <stdlib.h> int main() { char word[] = {'j','a','r','i','d'}; char let,i; for(i=0; i<10; i++){ printf("Guess a letter: "); scanf("%c", &let); if(let == 'j') printf("j****"); else if(let == 'a') printf("*..........*"); else if(let == 'r') printf("**r**"); else if(let == 'i') printf("***i*"); else if(let == 'd') printf("****d"); else printf("Guess again"); } system("pause"); }
Check this out. What if you had another array of chars that was all filled up with zeros, right. Now lets say your player pics the char, 'j'. Now what if you made a function that compares what the user put in with all of the leters in the target string, as well as what is in that second zeroed out memory buffer.
Ok, now it finds that you have never guessed 'j' before, but it finds that 'j' does exist in the target string. So it places a j in the answer buffer in the first index of the array.
Does this make sense? Its how I would go about this.
Yes that somewhat makes sense and that is how i believe it is supposed to be done but i am having trouble putting that into code
Ok, well I can show you how to do what I mean. Just don't go around thinking this means I am doing homework for you.
Example:
From here on out its all you buddy.Code:#include <stdio.h> #include <stdlib.h> int main() { char word[] = {'j','a','r','i','d'}; char *shadow; char let,i, shadow_length; shadow_length = sizeof word / sizeof word[0]; shadow = malloc(shadow_length + 1); shadow[shadow_length] = 0; for(i = 0; i < shadow_length; ++i) shadow[i] = '*'; for(i=0; i<10; i++){ printf("Guess a letter: "); scanf("%c", &let); if(let == 'j') printf("j****"); else if(let == 'a') printf("*..........*"); else if(let == 'r') printf("**r**"); else if(let == 'i') printf("***i*"); else if(let == 'd') printf("****d"); else printf("Guess again"); } system("pause"); }
Last edited by master5001; 10-24-2008 at 04:03 PM.
Haha thanks for that but we haven't learned about the string library so i'm not really sure what that does.
Also after each correct letter is guessed it needs to properly show which ones were guessed, i.e. if j and a were guessed it would display j..........*
Last edited by jstrike21; 10-24-2008 at 04:03 PM.
You may not want to use memset() though. It doesn't account for spaces, but since I am just trying to get you off to a start, its a direction to move in. I have to go though so I will leave you in tabstops' capable hands.