Problem with Hangman Code

I am having trouble running this Hangman code, the code is from http://xoax.net/ (i do NOT claim it to be my own in anyway), and is complete but when i compile and run it (using Dev-c++) the screen is black. (the code uses file reading, and the word .txt file is present so that isn't the problem) here is the code:

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>

unsigned int CountWords(const char*);
void ReadWords(const char*, std::string*);
void ClearScreen();
void DisplayMan0();
void DisplayMan1();
void DisplayMan2();
void DisplayMan3();
void DisplayMan4();
void DisplayMan5();
void DisplayMan6();

int main(int argc, const char* argv[])
{
using namespace std;

// Count words for allocation and read them in
const unsigned int kuiWordCount = CountWords(argv[1]);
string* qpWordArray = new string[kuiWordCount];
ReadWords(argv[1], qpWordArray);

// Seed the random number generator with the time
time_t qTime;
time(&qTime);
srand((unsigned int)qTime);

// An array of function pointers for output
void (*pfnaDisplayMan[])() = {DisplayMan0, DisplayMan1, DisplayMan2,
DisplayMan3, DisplayMan4, DisplayMan5, DisplayMan6};

bool bAnotherGame = true;
// Loop once per game
do {
// Pick a random word to guess and get its length
int iWordChoice = (rand() % kuiWordCount);
string& qrCurrWord = qpWordArray[iWordChoice];
const unsigned int kuiWordLength = qrCurrWord.length();

// Allocate an array to specify guessed letters
bool* bpGuessed = new bool[kuiWordLength];
for (unsigned int uiI = 0; uiI < kuiWordLength; ++uiI) {
bpGuessed[uiI] = false;
}

// Initialize game-ending variables
unsigned int uiWrongGuesses = 0;
bool bGuessedWord = false;
bool bGameOver = false;
// Main game loop
do {
// Clear the screen and output the man
ClearScreen();
pfnaDisplayMan[uiWrongGuesses]();

// If there have been 6 wrong gueses or the
// word has been guessed then the game is over.
if (uiWrongGuesses >= 6) {
cout << "You Lost!" << endl;
cout << "Answer = " << qrCurrWord << endl;
bGameOver = true;
} else if (bGuessedWord) {
cout << qrCurrWord << endl;
cout << "You Won!" << endl;
bGameOver = true;
} else {
// Output '*' or the letters that have been guessed
for (unsigned int uiI = 0; uiI < kuiWordLength; ++uiI) {
if (bpGuessed[uiI]) {
cout << qrCurrWord[uiI];
} else {
cout << '*';
}
}
cout << endl;
char cGuess = 0;
// Get the next guess
cout << "Next Guess? ";
cin >> cGuess;

// Check whether the guess is in the word and fill in
bool bIsGuessInWord = false;
for (unsigned int uiI = 0; uiI < kuiWordLength; ++uiI) {
if (!bpGuessed[uiI] && (qrCurrWord[uiI] == cGuess)) {
bpGuessed[uiI] = true;
bIsGuessInWord = true;
}
}

// Increment wrong guesses, if needed
if (!bIsGuessInWord) {
++uiWrongGuesses;
}

// Check whether all of the letters have been guessed
bGuessedWord = true;
for (unsigned int uiI = 0; uiI < kuiWordLength; ++uiI) {
if (!bpGuessed[uiI]) {
bGuessedWord = false;
}
}
}
} while (!bGameOver);

delete [] bpGuessed;

// Another game?
cout << "Play Again? (y/n) ";
char cPlayAgain = ' ';
cin >> cPlayAgain;
if (cPlayAgain == 'n') {
bAnotherGame = false;
}
} while (bAnotherGame);

delete [] qpWordArray;
return 0;
}

unsigned int CountWords(const char* kcpFileName) {
using namespace std;
unsigned int uiStringCount = 0;
ifstream qFile(kcpFileName);
while (!qFile.eof()) {
string qWord;
qFile >> qWord;
// Don't count empty strings
if (!qWord.empty()) {
++uiStringCount;
}
}
qFile.close();
return uiStringCount;
}

void ReadWords(const char* kcpFileName, std::string* qpWordArray) {
using namespace std;
unsigned int uiStringCount = 0;
ifstream qFile(kcpFileName);
while (!qFile.eof()) {
string qWord;
qFile >> qWord;
// Don't store or count empty strings
if (!qWord.empty()) {
qpWordArray[uiStringCount] = qWord;
++uiStringCount;
}
}
qFile.close();
}

void ClearScreen() {
using namespace std;
for (unsigned int uiI = 0; uiI < 20; ++uiI) {
cout << endl;
}
}

void DisplayMan0() {
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
}

void DisplayMan1() {
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
}

void DisplayMan2() {
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /" << endl;
cout << "|" << endl;
cout << "|" << endl;
}

void DisplayMan3() {
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X" << endl;
cout << "|" << endl;
cout << "|" << endl;
}

void DisplayMan4() {
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X\\" << endl;
cout << "|" << endl;
cout << "|" << endl;
}

void DisplayMan5() {
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X\\" << endl;
cout << "| /" << endl;
cout << "|" << endl;
}

void DisplayMan6() {
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X\\" << endl;
cout << "| / \\" << endl;
cout << "|" << endl;
}
Last edited on
BUMP.
nobody will reply to this, put your code in code brackets
Works fine with me.
You should have a look at argv and what it means. see http://www.cprogramming.com/tutorial/c/lesson14.html

spoiler #1:
The argv[1] is the filename it is supposed to load. It is provided to the main function. That means you have to provide it when you open the program.
spoiler #2
To do this in windows go to start-->search/find and type cmd. (Or find another way to open this cmd.exe program.) Then, in the cmd.exe window that pops up, go to the folder both the hangman.exe and the word.txt files are in. (I presume these are the names.) Then type
hangman word.txt

Now the program loads the words in the .txt file and should operate fine.
Hope this works.

P.S. you could also replace all the argv[1]'s to "word.txt" (within double quotation marks) and see whether it works. (Do not change the argv[] in the main function head.)
Last edited on
i ran the cmd.exe with the txt file and it says hangman isnt recognized as a file, operable program or command, idk why, all these files are in the same drive
Last edited on
Hey methodos I just replaced all argv[1]'s to "word.txt"and it still isnt working. The thing is i downloaded this code from the site i listed above, the code is supposed to be complete and working, as you said, and when i compile and run it, it wont let my input anything at all, i type any key and it wont budge or output anything on the screen. methodos are you using Dev-c++ or visual studio when you run it? can you tell me the exact same steps you took when you successfully ran it?
1. I am using Visual Studio, so I included "stdafx.h", but that shouldn't matter.
2. I built the .exe. (Actually debugged but that builds the project and then tries to run it without the word.txt parameter which gives an error message in Visual Studio.)
3. I made a folder hangman in c:/ and copied the hangman.exe to it. Then I made a word.txt file and typed two words in there (followed by enter).
4. I opened the command promtp (cmd.exe) and navigated to c:/hangman/
5. then i typed
hangman word.txt.
I will have a look at the replacing argv[1] to word.txt later today. There are some possibilities there though.
To get it to work without cmd (and only with the words in the word.txt file) you should change the two instances of
ifstream qFile(kcpFileName)
to
ifstream qFile("word.txt");

if that doesn't work, give the full path of the word.txt file, like
ifstream qFile("c:/folder/subfolder/hangman/word.txt");

Good luck!
Last edited on
Thx methodos, i haven't tried your latest suggestion yet, gonna try it later tonight and let you know if it works
Just replaced kcpfilename instances with "word.txt" as methodos suggested, but I still get a blank screen and still am unable to output.
What's the code look like now?
Did you replace only the kpcfilenames in ifstream qFile()? You shouldn't replace those in CountWord() and ReadWords().
the code now looks like this. I ran it on both dev-c++ and visual studio 2010, it says no errors, but the screen is blank, I also made a word.txt file with two words and pressed enter

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>

unsigned int CountWords(const char*);
void ReadWords(const char*, std::string*);
void ClearScreen();
void DisplayMan0();
void DisplayMan1();
void DisplayMan2();
void DisplayMan3();
void DisplayMan4();
void DisplayMan5();
void DisplayMan6();

int main(int argc, const char* argv[])
{
using namespace std;

// Count words for allocation and read them in
const unsigned int kuiWordCount = CountWords(argv[1]);
string* qpWordArray = new string[kuiWordCount];
ReadWords(argv[1], qpWordArray);

// Seed the random number generator with the time
time_t qTime;
time(&qTime);
srand((unsigned int)qTime);

// An array of function pointers for output
void (*pfnaDisplayMan[])() = {DisplayMan0, DisplayMan1, DisplayMan2,
DisplayMan3, DisplayMan4, DisplayMan5, DisplayMan6};

bool bAnotherGame = true;
// Loop once per game
do {
// Pick a random word to guess and get its length
int iWordChoice = (rand() % kuiWordCount);
string& qrCurrWord = qpWordArray[iWordChoice];
const unsigned int kuiWordLength = qrCurrWord.length();

// Allocate an array to specify guessed letters
bool* bpGuessed = new bool[kuiWordLength];
for (unsigned int uiI = 0; uiI < kuiWordLength; ++uiI) {
bpGuessed[uiI] = false;
}

// Initialize game-ending variables
unsigned int uiWrongGuesses = 0;
bool bGuessedWord = false;
bool bGameOver = false;
// Main game loop
do {
// Clear the screen and output the man
ClearScreen();
pfnaDisplayMan[uiWrongGuesses]();

// If there have been 6 wrong gueses or the
// word has been guessed then the game is over.
if (uiWrongGuesses >= 6) {
cout << "You Lost!" << endl;
cout << "Answer = " << qrCurrWord << endl;
bGameOver = true;
} else if (bGuessedWord) {
cout << qrCurrWord << endl;
cout << "You Won!" << endl;
bGameOver = true;
} else {
// Output '*' or the letters that have been guessed
for (unsigned int uiI = 0; uiI < kuiWordLength; ++uiI) {
if (bpGuessed[uiI]) {
cout << qrCurrWord[uiI];
} else {
cout << '*';
}
}
cout << endl;
char cGuess = 0;
// Get the next guess
cout << "Next Guess? ";
cin >> cGuess;

// Check whether the guess is in the word and fill in
bool bIsGuessInWord = false;
for (unsigned int uiI = 0; uiI < kuiWordLength; ++uiI) {
if (!bpGuessed[uiI] && (qrCurrWord[uiI] == cGuess)) {
bpGuessed[uiI] = true;
bIsGuessInWord = true;
}
}

// Increment wrong guesses, if needed
if (!bIsGuessInWord) {
++uiWrongGuesses;
}

// Check whether all of the letters have been guessed
bGuessedWord = true;
for (unsigned int uiI = 0; uiI < kuiWordLength; ++uiI) {
if (!bpGuessed[uiI]) {
bGuessedWord = false;
}
}
}
} while (!bGameOver);

delete [] bpGuessed;

// Another game?
cout << "Play Again? (y/n) ";
char cPlayAgain = ' ';
cin >> cPlayAgain;
if (cPlayAgain == 'n') {
bAnotherGame = false;
}
} while (bAnotherGame);

delete [] qpWordArray;
return 0;
}

unsigned int CountWords(const char* kcpFileName) {
using namespace std;
unsigned int uiStringCount = 0;
ifstream qFile("word.txt");
while (!qFile.eof()) {
string qWord;
qFile >> qWord;
// Don't count empty strings
if (!qWord.empty()) {
++uiStringCount;
}
}
qFile.close();
return uiStringCount;
}

void ReadWords(const char* kcpFileName, std::string* qpWordArray) {
using namespace std;
unsigned int uiStringCount = 0;
ifstream qFile("word.txt");
while (!qFile.eof()) {
string qWord;
qFile >> qWord;
// Don't store or count empty strings
if (!qWord.empty()) {
qpWordArray[uiStringCount] = qWord;
++uiStringCount;
}
}
qFile.close();
}

void ClearScreen() {
using namespace std;
for (unsigned int uiI = 0; uiI < 20; ++uiI) {
cout << endl;
}
}

void DisplayMan0() {
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
}

void DisplayMan1() {
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
}

void DisplayMan2() {
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /" << endl;
cout << "|" << endl;
cout << "|" << endl;
}

void DisplayMan3() {
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X" << endl;
cout << "|" << endl;
cout << "|" << endl;
}

void DisplayMan4() {
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X\\" << endl;
cout << "|" << endl;
cout << "|" << endl;
}

void DisplayMan5() {
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X\\" << endl;
cout << "| /" << endl;
cout << "|" << endl;
}

void DisplayMan6() {
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X\\" << endl;
cout << "| / \\" << endl;
cout << "|" << endl;
}
The program cannot find the word.txt file. Just supply the full path of the file, like I suggested before. for instance, i made a word.txt with words in it and saved it directly to my c: drive. In that case you would write
ifstream qFile("c:/word.txt");
and the file will be found and opened by the program. If that doesn't work I am out of options.

And please supply your code sanmples on this forum in code blocks like tkauffman "suggested." This is done by typing "code" within square brackets (and without quotation marks), then the code and close the code block by typing "/code" within square brackets. alternatively, click the <> button at the right of the reply block and write the code inbetween.
It's appreciated by many on this site if you do so.
Just replaced the two instances with ifstream qFile("c:/word.txt"); it still didn't work, thanks for you help anyway methodos

You didn't mention anything about placing word.txt directly on your c:/ drive. Where is the file word.txt?
Oh sorry i forgot to mention that, i directly placed it in the c:/drive and it still, does nothing.
A little late, you probably dont need it anymore, but I just wanted to add that it might be that your file.txt contains a space character.
methodos, can you please help me with my other thread, the link is below

http://cplusplus.com/forum/general/42843/ Please help me!
Topic archived. No new replies allowed.