PDA

Click to See Complete Forum and Search --> : Beginner needs help!


Lee M.
Mar 2nd, 2001, 04:52 PM
I have a homework assignment due tonight;

I need to write a program in C++ with the following:

Hangman Game

It has to have 2 parallel arrays, One named Word, which will store the word entered by a user.

The second array will be called Guesses, will store a corisponding dash (-) for each letter in the word.

with each correct guess the (Hyphen) will be replaced by the letter.

Both the Guess and the word array will need to be 6 element Character arrays.

Now I fumbled my way through the simple array project, and this is my last problem, and I really need help. The book isn't really clear. I just want to finish the class and move on, it was required. I have no plans on writing in C++, I am O.K. with VB.


Help! I really need to get it done, and I have NO CLUE!

Mar 2nd, 2001, 05:25 PM
Well, I don't think people on here would do your assignment for you, but we'd be glad to help where ever you are having trouble. What are you stuck on right now?

Lee M.
Mar 2nd, 2001, 05:28 PM
I wasn't looking for that. I am confused on the parallel array and how to even begin writing it

Mar 2nd, 2001, 05:41 PM
char cWord[5]; // 6 elements including 0
char cUnderscores[5];

cWord = "Myword";
cUnderscores = "_____";

Lee M.
Mar 2nd, 2001, 06:31 PM
Never mind.
I know how to initialize the array.
I needed some insights on how the parallel arrays work.

I will look elsewhere.

Lee M.
Mar 2nd, 2001, 11:56 PM
Megatron,
First off let me say I didn't mean to sound like a dick.

After the quake scrambled my hard drive, let's just say I was in poor spirits.

I got the assignment done, and still have no clue how I did it.

Things here in Seattle have been a little hectic lately.

I hope you were not offended, (for too long).
Lee

HarryW
Mar 3rd, 2001, 03:03 AM
You need to declare the char array with a size equal to the number of characters you want in your string, plus 1 for the end of string character ('\0'). You also can't just assign string literals to char arrays (a string literal like "Myword" in your code is basically a pointer to that string in the .exe), you need to use the strcpy() function to copy them over.


char cWord[7]; // 6 elements plus the '\0'
char cUnderscores[7];

strcpy(cWord, "Myword");
strcpy(cUnderscores, "_____");


I think the idea with the parallel arrays is that when a user guesses a letter, you can loop through your arrays and check in the cWord[] array whether that letter occurs. If it does then you set the equivalent letter in the cUnderscores array to the letter it's supposed to be. You can use a loop to do that for all the letters in the arrays. If the character wasn't found then it was a bad guess and you need to do something like decrement the number of lives they have left.

Using the parallel arrays is quite a good idea, it's not hard to use that to do all your game logic.

Lee M.
Mar 3rd, 2001, 09:59 AM
Thanks Guys!