#include "stdafx.h"
#include <time.h>
#include <stdlib.h>
#include <iomanip>
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <stdio.h>
#include <cstdlib>
#include <sstream>
#include <vector>
using namespace std;

int main();

class Hangman
{
public:
	void set_values (string, string, string, string, string, string, string, string, string, string, string, string, string, string);
	void randomWordGenerator(int);
	void letterCount();
	void blankSpaces();
	void welcomeMessage();
	void yesOrNo();
	void randomNumberGenerator();
	void guessCount();
	//****new function to process correct guesses
private:
	string line[13];//set_values
	string chosenWord;//randomWordGenerator
	int strsz;//letterCount
	char input;//welcomeMessage
	int random_integer;//randomNumberGenerator
	char guess[5];//guessCount record of guesses
	
	int h; //guessCount guess position
	
	
} man;
void Hangman::guessCount()
{
	for(h=0;h<=5;h++)
	{
	cout << "Make a guess: ";
	cin >> guess[h];
	man.blankSpaces();
	}
}

void Hangman::yesOrNo()
{
	if (input == 'Y' || input == 'y')
		{
			man.randomNumberGenerator();
			man.randomWordGenerator(random_integer);
			man.letterCount();
			man.blankSpaces();
			man.guessCount();
			main();
		}
		else
		{
			cout << "Goodbye." << endl;
			system ("PAUSE");
		}
}

void Hangman::randomNumberGenerator()
{
	srand((unsigned)time(0));
	random_integer = (rand()%10)+1;
}

void Hangman::welcomeMessage()
{
	cout << "Welcome to the hangman game!" << endl;
	cout << "Would you like to play? (Y/N)" << endl;
	cin >> input;
}

void Hangman::blankSpaces()
{
	cout << "Your word to guess: ";
	int k;
	for (k=0; k<strsz; k++)
	{	

		if (chosenWord[k] == guess[h])
		{
			cout << guess[h] << " ";
		}
		else
		{
			cout << "_ ";
		}
		
	}
	cout << setw(30) << "Letters guessed so far: ";
	int p;
	for(p=0;p<=5;p++)
	{	
		cout << guess[p];
	}
	cout << endl;
}

void Hangman::letterCount()
{
	strsz = chosenWord.length();
	cout << chosenWord << " " << strsz << endl;
}

void Hangman::randomWordGenerator(int e)
{
	int i;
	for(i=0; i<=13; i++)
	{
		if ( e == i )
		{
			chosenWord = line[i];
		}
	}

}
void Hangman::set_values (string a, string b, string c, string d, string e, string f, string g, string h, string i, string j, string k, string l, string m, string n)
{
	line[0]=a;
	line[1]=b;
	line[2]=c;
	line[3]=d;
	line[4]=e;
	line[5]=f;
	line[6]=g;
	line[7]=h;
	line[8]=i;
	line[9]=j;
	line[10]=k;
	line[11]=l;
	line[12]=m;
	line[13]=n;	
}

void Initialize ()
{
	string line[14];
	ifstream wordfile; 
	wordfile.open("words.txt");
	while( !wordfile.eof() )
	{

		int i;
		for (i=0; i<=13; i++)
		{
			std::getline (wordfile, line[i]);
		}
		man.set_values(line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7], line[8], line[9], line[10], line[11], line[12], line[13]);
		}
}



int main()
{		
	Initialize(); 
	man.welcomeMessage();
	man.yesOrNo();
	return 0;
}

First, everyone should know that this program works, the question is not about some run time error or to debug. The program's purpose is to play a hangman game. The entire program is not complete yet, I'm just taking it step by step for now. As written, the program does the following:

1. the main() functions calls on the initialize() function which opens up a word document with a bunch of random words and stores all of these words (14 words total) into a string array via calling the functions man.set_values().

2. the class Hangman stores all of these words from the file as a string array known as line[].

3. the program goes back to main() to call the next function man.welcomeMessage() which asks the user if he/she wants to play then records the input (y or n) to the variable "char input" (which is declared in the Hangman class definition)

4. the program goes back to main to call on the next function man.yesOrNo() which processes the y or n input. if n, the program outputs "Goodbye" and the program ends.

5. When the user inputs 'y', the man.yesOrNo() function enters the if statement and calls on man.randomNumberGenerator() and man.randomWordGenerator(). These two functions create a random number and stores it to a private Hangman class variable and generates a random word (from the line[] string array) by using the random number to 'randomly' choose a word from the line[] array.

6. The random word is saved as the variable string chosenWord

7. the program continues back to man.yesOrNo() functions which calls the next function man.letterCount() which finds the length of chosenWord and saves that value as int strsz

8. back to man.yesOrNo(), which calls the next function in the nested if statement: man.blankSpaces() which outputs "Your word to guess: " and then uses the variable strsz and a for/if loop to determine the amount of blank spaces to print (printed as _ _ _ ect.) *since this is the first loop and a guess has not been inputted yet, man.blankSpaces() will print out all blanks for the first time around ( for ex. if chosenWord = pepsi, this function would output "Your word to guess: _ _ _ _ _" the first time around)

9. back to man.yesOrNo() which calls the next function man.guessCount() which asks the user for a guess and stores that single character letter guess into variable char guess[5] (declared and initialized by the Hangman class definition). since this is in a for loop set to end after 6 loops (the user gets 6 guesses) the user inputs are stored in order, so first guess = guess[0], second guess = guess[1], ect ect

10. from the man.guessCount() function, after the first guess input is recieved, the man.blankSpaces() function is called again.

11. the difference this time is that the char guess[5] array now has a value in it at guess[0], which the function man.blankSpaces() compares to each letter of chosenWord and will print out the letter in place of the blank spaces (underscores)

12. the program does this six times then restarts the entire program and asks if the user wants to play again.

the output looks like this:

Welcome to the hangman game!
Would you like to play? (Y/N)
y
(the chosenWord=persistent)
Your word to guess: _ _ _ _ _ _ _ _ _ _ Letters guessed so far:
Make a guess: p
Your word to guess: p _ _ _ _ _ _ _ _ _ Letters guessed so far: p
Make a guess: e
Your word to guess: _ e _ _ _ _ _ e _ _ Letters guessed so far: pe
Make a guess: t
Your word to guess: _ _ _ _ _ _ t _ _ t Letters guessed so far: pet
Make a guess: f
Your word to guess: _ _ _ _ _ _ _ _ _ _ Letters guessed so far: petf
Make a guess: s
Your word to guess: _ _ _ s _ s _ _ _ _ Letters guessed so far: petfs
Make a guess: b
Your word to guess: _ _ _ _ _ _ _ _ _ _ Letters guessed so far: petfsb
Welcome to the hangman game!
Would you like to play? (Y/N)

As you can see there is a undesired effect here. As is, the code does print out the correctly guessed letters in the correct spots among the blank spaces, but when a new guess in inputted, the previous guess is forgotten and the new correct guess is printed out without the previous correct guesses being printed out with it.

The code SHOULD print out the following instead:

Welcome to the hangman game!
Would you like to play? (Y/N)
y
(the chosenWord = persistent)
Your word to guess: _ _ _ _ _ _ _ _ _ _ Letters guessed so far:
Make a guess: p
Your word to guess: p _ _ _ _ _ _ _ _ _ Letters guessed so far: p
Make a guess: e
Your word to guess: p e _ _ _ _ _ e _ _ Letters guessed so far: pe
Make a guess: t
Your word to guess: p e _ _ _ _ t e _ t Letters guessed so far: pet
Make a guess: f
Your word to guess: p e _ _ _ _ t e _ t Letters guessed so far: petf
Make a guess: s
Your word to guess: p e _ s _ s t e _ t Letters guessed so far: petfs
Make a guess: b
Your word to guess: p e _ s _ s t e _ t Letters guessed so far: petfsb
Welcome to the hangman game!
Would you like to play? (Y/N)

I'm having a hard time figuring out what code I need to put in so that previous correct guesses are stored in a string relative to blank spaces not guessed yet.

I have already figured out how to store all guesses and output them, as seen with the line "Letters guessed so far: " but I need to be able to select the correct guesses and store them into another array and store them with underscore/blankspaces in the appropriate spots, or another method that would output the above posted correct guesses.

The function that I suspect needs the alterations is man.blankSpaces() or at least it makes the most sense to have this function save this new 'correct guesses' array.

Any ideas?

Edited 7 Years Ago by ramsdellcp: added some clarification

Your code has probelms in function Hangman::blankspaces(). In it you are matching every character of the chosen character with the last guessed character and printing _ or the character for the whole word every time. thats the reason it is showing only the last selected character's place values if any.

What you need to do is to match every character of the choosen word with every guessed character till now and print accordingly.Here is a modified version of your code which may work ... try it...

void Hangman::blankSpaces()
{
    cout << "Your word to guess: ";
    int k,i,flag;
    for (k=0; k<strsz; k++)
    {
        flag=0;
        for(i=0;i<=h;i++)
        if (chosenWord[k] == guess[i])
        {
             flag=1;
             break;
        }
       
        if(flag) 
              cout << guess[i] << " ";
        else
              cout << "_ ";
    }
    cout << setw(30) << "Letters guessed so far: ";
    int p;
    for(p=0;p<=5;p++)
    {
        cout << guess[p];
    }
     cout << endl;
}

Edited 7 Years Ago by csurfer: n/a

YES YOU ARE AMAZING. the amended code works perfectly!!! Thank you so much sir.

just a quick question, if I wanted to make a anti statement to if(flag), would that be if(!flag) ?

This article has been dead for over six months. Start a new discussion instead.