Archived

This topic is now archived and is closed to further replies.

Coding Issues(DOS Hangman)

This topic is 5132 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ive been coding a little hangman game in DOS to get me started in C++ but i have just 1 issue i cant solve... When you type a bit more letters, the Tries you get turns into a big value and i dunno where its coming from. Code...
#include <iostream.h>
#include <string.h>
#include <stdlib.h>

#define UNKNOWN '*'
#define HANGMANWORD "Hangman"
#define TRIES 5

void Hangman();

void main ()
{
	Hangman();
}

void Hangman () {
	cout << "Now loading(Hangman)...\n\n";
	char word[] = HANGMANWORD, hangword[] = HANGMANWORD;
	//Cover Word...
	int wordlen = strlen(word);
	for (int len = 0; len < wordlen; len++)
		hangword[len] = UNKNOWN;
	bool inGame = true, win = false;
	int guesses = TRIES;
	if (guesses < 3)
		guesses = 3;
	char buffer[] = "", letter;
	//StartGame	Loop
	while (inGame) {
		cout << "\nHidden Word - (" << hangword << ")\tTries Left - (" << guesses << ")\n";
		cout << "Pick A Letter - ";
		bool match = false;
		cin >> buffer;
		letter = buffer[0];
		if (strlen(buffer) == 1) {
			for (int len = 0; len < wordlen; len++) {
				if (toupper(letter) == toupper(word[len])) {
					hangword[len] = word[len];
					match = true;
				}
			}
			win = true;
			for (len = 0; len < wordlen; len++) {
				if (hangword[len] != word[len]) {
					win = false;
				}
			}
			if (match != true)
				guesses--;
			if (guesses <= 0) {
				cout << "GameOver...\n\n";
				inGame = false;
			}
			if (win == true) {
				cout << "You win...\n\n";
				inGame = false;
			}
		}
		else {
			cout << "You must pick 1 letter...\n\n";
		}
	}
}
   
this is my first C++ project... [edited by - DarkSephiroth on November 27, 2003 12:26:26 AM]

Share this post


Link to post
Share on other sites
You should not define buffer as char[] = "".
This will create a string with no length. As soon as you enter some chars to that string you are writing somewhere in memory (obviously into the guesses variable).

Simple change:

Define a max string length and define buffer as
char buffer[MAX_STRING_LENGTH];

Since you''re using C++ already, why not try to use std::string?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ok,

There are plenty of problems with your code.
The first is that you don''t allocate any memory for the buffer variable.

This will allocate 0 bytes to buffer.
char buffer[] = ""

So when you type in a letter into buffer you will directly get memory leaks.

Do like this instead.
char buffer[100]; // This will allow up 100 chars

This will get your program working.

Then you should have a look at your compare code.
for (len = 0; len < wordlen; len++) {
if (hangword[len] != word[len]) {
win = false;
}
}

There are better ways to compare strings!!!

Then when you get it all to work I suggest you do it all again from scratch and see if you can come up with a cleaner and smarter solution. Read up on string handling and memory allocation.

I''d rather see a bool Hangman(char* hangword,int tries) function instead of all those #defines


Cheers,



Share this post


Link to post
Share on other sites
Guest Anonymous Poster
He probably means a console application from Windows and not actually DOS.

Share this post


Link to post
Share on other sites
quote:
....

why not just put it all in main?


I think he''s doing it to isolate the game from the app as much as possible. Obviously it''s a matter of preference, but when projects get bigger, organization will save many hours of debugging.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
He probably means a console application from Windows and not actually DOS.


I still use my good ole dos compiler once in a while, while I do the majority of my programming under msvc now, I still use my turbo c/c++ compiler every now and again.

Share this post


Link to post
Share on other sites