// AY3814 - Chris Ramsdell.  Program 1 - Hangman game per guidelines.

#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[20];//guessCount record of guesses
	string guessString[12];//blankSpaces stores correctly guessed letters
	int h;//guessCount dictates what slot guesses go into the guess
	int inCorrectGuess; // keeps track of how many incorrect guesses have been made

} man;
void Hangman::guessCount()
{
	for(h=0; h<=20; h++)
	{
		if (inCorrectGuess <=5) // need to find a way to implement incorrectguess so it increments on every wrong guess
		{						
		cout << "Make a guess: ";
		cin >> guess[h];
		
		man.blankSpaces();
		}
		
	}
}

void Hangman::yesOrNo()
{
	if (input == 'Y' || input == 'y')
		{
			man.randomNumberGenerator();
			man.randomWordGenerator(random_integer);
			inCorrectGuess = 0; // initializes incorrect guesses to 0, will also reset to 0 for second round games
			man.letterCount();
			man.blankSpaces();
			man.guessCount();
			int v;
			for (v=0; v <=5; v++) // resets the stored guesses to nil value before restarting game
			{
				guess[v] = '\0';
			}
			main();
		}
		else
		{
			cout << "Goodbye." << endl;
			system ("PAUSE");
		}
}

void Hangman::randomNumberGenerator()
{
	srand((unsigned)time(0));
	random_integer = (rand()%14)+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,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 q;
    for(q=0;q<=20;q++)
    {
       cout << guess[q];
    }
	
    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;
}

This program works, it is a hangman game. the problem I am having is that I can't figure out how to stop the game after 6 wrong guesses have been made. as is, the program just lets the user guess as many times as he wants (up to 20) with no consequence for making wrong guesses.

this little snippet from the above code has the variable inCorrectGuess which i need to be able to increment on the event that a wrong guess is made. I've tried so many different things but nothing is working

void Hangman::guessCount()
{
	for(h=0; h<=20; h++)
	{
		if (inCorrectGuess <=5) // need to find a way to implement incorrectguess so it increments on every wrong guess

please help!!

You want to run the game while the number of incorrect guesses is less than 7, and you want to increment the number if incorrect guesses each time the user makes a wrong guess.
So, each time the user makes a wrong guess you should do:

++inCorrectGuess;

As for the first part, put your code that exectes the game in a while loop:

while(inCorrectGuess < 7)
{............
......
}

I hope that helps clarify your problem.
Don't post the same question 3 times in the future.

Edited 7 Years Ago by Grn Xtrm: n/a

Thank you for your reply. I understand the need for a while loop but what I still don't understand is how to actually write the syntax to recognize a incorrect guess and increment the inCorrectGuess counter accordingly. I tried this, but it fails horribly. first, it never increments on wrong guesses and also for some reason it clears out the guess string (or at least I think it does) because no previous guesses are being printed after "Letters guessed so far: "

void Hangman::blankSpaces()
{   cout << "Your word to guess: ";
    int k,i,flag,p;
	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 << "_ ";
	}
//**********************************************************
	for (p=0;p<strsz;p++)
	{
		if(guess[h] =! chosenWord[p])
		{
			counter++;
		}
	}
	if(counter == strsz)
	{
		cout << "incremented" << endl;
		inCorrectGuess++;
	}
//**********************************************************
    cout << setw(30) << "Letters guessed so far: ";
    int q;
    for(q=0;q<=20;q++)
    {  cout << guess[q];
    }
    cout << endl;
}

Hey you are posting every problem related to this hangman here at daniweb.Try something on your own.You'll learn a lot.

And indent the code properly so that its easy to read...

void Hangman::blankSpaces()
{   cout << "Your word to guess: ";
    int k,i,flag,p;
	for (k=0; k<strsz; k++)
      {  
       flag=0;
       for(i=0;i<=h;i++)
	   		if (chosenWord[k] == guess[i])
			{	flag=1;
				break;
			}
	if(flag)  // Wrongly indented in your code.
	{	cout << guess[i] << " ";
	}
	else
	cout << "_ ";
	}

Use your inCorrectguess variable here in some form may be as:

void Hangman::blankSpaces()
{   cout << "Your word to guess: ";
    int k,i,flag,p,correct_guesses=0;
	for (k=0; k<strsz; k++)
      {  
       flag=0;
       for(i=0;i<=h;i++)
	   		if (chosenWord[k] == guess[i])
			{	flag=1;
				break;
			}
	if(flag)
	{	
                   correct_guess++; // Correct_guess increments on every correct guess that is flag is set
                   cout << guess[i] << " ";
	}
	else
	cout << "_ ";

        inCorrectguess=h-correct_guess; //Here you'll get the total wrong guess count.
        // Use it in what ever form here....
}

Edited 7 Years Ago by csurfer: n/a

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