Hangman game with strange bug.

I can't seem to track down where the issue with this program is. I've only just started coding C++ any help would be great.

I started off with two global char variables as I intended to use functions and wanted not to have to worry about inheritance or writing local variables. Problem was when I finished the code I realised functions weren't needed and moved the variables within int main(). Now the variables are full of ASCII characters.

Here's the code that works:

// Hangman game using 9 preset words

#include<iostream>

#include<cstring>

usingnamespace std;

char word[40];

char guess[40];

int main(){

char game;

int n;

char L;

int lives = 7;

//Menu system

for(;Wink{

do{

cout <<"The C++ Hangman Game. \n";

cout <<"Choose a game number (1-9, q to quit)";

cin >> game;

}while( game <'1' || game >'9' && game !='q');

if (game =='q')return 0;

//Setting the word for the hangman game:

switch(game){

case'1':

strcpy(word,"namespace");

break;

case'2':

strcpy(word,"switch");

break;

case'3':

strcpy(word,"array");

break;

case'4':

strcpy(word,"function");

break;

case'5':

strcpy(word,"recursion");

break;

case'6':

strcpy(word,"pointer");

break;

case'7':

strcpy(word,"string");

break;

case'8':

strcpy(word,"c++");

break;

case'9':

strcpy(word,"double");

break;

}

//once choice is made exit menu

break;

}

n = strlen(word);

for(n; n != 0; n--) strcat(guess,"_");

/* Now there are 2 strings that contain the word to guess and the current guess (all _s). */

// game start proper

for(;Wink{

cout <<"Here's the word: " << guess;

cout <<"\nLives = " << lives;

cout <<"\nEnter a letter to guess: ";

cin >> L;

char *w;

char *g;

int right_wrong = 0;

w = word;

g = guess;

//check guess letter against word and copy over if right. Lose a life if wrong

while(*w){

if(*w == L){

*g = L;

right_wrong = 1;}

w++;

g++;

}

if(right_wrong)

cout <<"You're right\n";

elseif(!right_wrong){

cout <<"You're wrong\n";

lives--;}

// Win-lose condition check and exit

if(lives <= 0){

cout <<"\nYou lose!\n";

cout <<"Out of lives. The word was "<< word <<"\n";

break;}

if(!strcmp(word, guess)){

cout <<"You win!\n" <<"Word was " << word <<"\n";

break;}

}

return 0;

}

[6114 byte] By [MonkeyNeedBanana] at [2008-1-10]
# 1

Your problem is that when an array is a global variable it is initialized to all zeros when the program starts. But when it is a local variable of a function, like main, the variable is not initialized so you just pick up whatever was on the stack previously - i.e. random garbage. So you need to explicitly initialize all your local variables. For an array I would do something like:

Code Snippet

char word[40] = { 0 };

char guess[40] = { 0 };

Some other notes:

In C++ C-style strings need to be null-terminated but your code below doesn't copy the terminating character.

Code Snippet

n = strlen(word);

for(n; n != 0; n--) strcat(guess,"_");

I would consider using the bool type for right_wrong.

Also you might consider using std:: string instead of arrays - this would get rid of the need write code to explicitly copy the strings.

JonathanCaves-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C++ 2005 Express Edition...
# 2
Nah, strcat() will properly zero-terminate. He does however forget to reinitialize guess in his outer game loop so guess will get longer and longer until it destroys the stack.
nobugz at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C++ 2005 Express Edition...
# 3

Everything works fine now with that change to word = { 0 }. I had a feeling it was a garbage assignment but didn't know how to fix it.

Thanks for the pointer on the bool type. I was basically using and int as a bool and that's probably a bad habit to get into.

The other things you added and the points by nobugz will probably make a lot more sense in a few weeks. This was just a test I set myself after learning some basic commands to build and debug a program using them. I feel like I can move on to more complex stuff now.

Thanks again for the help.

MonkeyNeedBanana at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C++ 2005 Express Edition...