Hangman - Game doesn't end even when all blanks are full

I'm writing this Hangman game and this is the function that i'm using to tell if the game is over:
1
2
3
4
5
6
7
8
9
10
11
12
13
//blanks [] is the array of blank spaces i.e. _ _ _ a _ _ in hangman
//length is the number of elements in the array.

bool isGameOver(char blanks[],int length)
{
	int guessed;
	for(int i=0;i<length;i++)
	{
		if(blanks[i]!='_'){guessed++;}
	}
	if (guessed==length){return true;}
	else return false;
}


The problem is that even when you guess all the letters in the game, it does end the game as it's supposed to - it continues to run. I've tried to look for the faults but I haven't found any. Could you please tell me where I've gone wrong.

Main Function and other possibly relevant functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>

#define MAX_WRONG 8

using namespace std;

int getRandom(); //gets a random number based on which a word is chosen.
int getCategory();//prompts user to choose category of word
void getWord(int, int, char []);//gets word from a list of words
void emptyBlanks(char [],int); //assigns '_' to all elements in array blank[].
void drawBlanks(char [],int);
bool check(char [],char [],int,char); //returns true if letter is in the word
bool isGameOver(char[],int); //checks if the user has guessed all letters.
void showWinScreen(); //screen displayed when user wins.

int main()
{
	int random = getRandom();
	int category = getCategory();
	char word[100];
	getWord(random,category,word);
	int wrong = 0;
	int length = strlen(word);
	char blanks[length];
	emptyBlanks(blanks,length);	


	do
	{
		drawBlanks(blanks,length);
		cout<<"Wrong Guesses: "<<wrong<<" "<<random<<endl;
		cout<<"\nEnter a letter: ";		
		char ch;
		cin>>ch;
		if(check(blanks,word,length,ch)==false){wrong++;}
		if(isGameOver(blanks,length)==true){showWinScreen(); break;}
	}while(wrong<=MAX_WRONG);
		
	return 0;
}

void emptyBlanks(char blanks[],int length)
{
	for(int i=0;i<length;i++)
	{
		blanks[i]='_';
	}
}

void drawBlanks(char blanks[], int length)
{
	for(int i=0;i<length;i++)
	{
		cout<<blanks[i]<<" ";
	}
	cout<<endl;
}

Last edited on
Infact, line 39
if(isGameOver(blanks,length)==true){showWinScreen(); break;}

is not even read!
In function isGameOver, guessed is not initialised.
line 39 should be executed at least one time.

by the way, there is no need to compare a boolean value against true. And for false you could use not var or !var
Thanks! That did the trick.

i have one more question, though. when the user enters char ch, in line 36 and 37, if he enters say two letters, then both letters will be taken as two seperate inputs. I want the user to be able to only enter one letter at a time so I wrote this function
1
2
3
4
5
6
7
8
char getch()
{
	char ch;
	prompt: cin>>ch;
	if(isupper(ch)){ch=to(ch);} 
	if(sizeof(ch)!=1){cout<<"Single letter must be entered. Reenter letter: "; goto prompt;}
	return ch;	
}


But this doesn't work ! Where've I gone wrong here?
Don't use goto.
You got a logic problem. http://en.wikipedia.org/wiki/Sizeof
Read with a string or a char [], and ask for its length. (there are a lot of articles an threats about data validation in this page)
Topic archived. No new replies allowed.