#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!!