#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
	string guess;//guessCount record of guesses
	
	
} man;
void Hangman::guessCount()
{
	int h;
	for(h=0;h<=5;h++)
	{
	cout << "Make a guess: ";
	cin >> guess[h];//error up to this point on second loop	
		int e;
		for(e=0;e<strsz;e++)
			{
				if (chosenWord[e] == guess[h])
				{
					cout << "guessCount success!" << endl;
					//call another function to process correct guess
				}
			}
	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++)
	{
		cout << "_ ";
	}
	cout << setw(30) << "Letters guessed so far: ";
	int p;
	for(p=0;p<=guess.length();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;
}

This code outputs the following before producing a error:

Welcome to the hangman game!
Would you like to play? (Y/N)
y //input to input
plum 4 //this line is printed strictly for reference and will not be in (the word to guess and the number of letters in the word) the actual finished program
Your word to guess: _ _ _ _ Letters guessed so far:
Make a guess: l // input to guess
guessCount success! //verification that the guess matched a letter
Your word to guess: _ _ _ _ Letters guessed so far: l
Make a guess: // errors out here with this error:

Expression: string subscript out of range

I have pinpointed the error to

cin >> guess[h]; in the void Hangman::guessCount() function (line 47)

I have a feeling that the error is coming from trying to put in the input to guess[1] on the second loop and I'm not experienced enough to recognize what that error would be.

I think I need to implement vectors on the string guess array but it seems to produce even more errors. I guess I do not understand why the guess array cannot take a input at guess[1].

Any help would be appreciated, Thanks

Recommended Answers

All 3 Replies

why do you need to keep all the old guesses? If the guess is wrong then just toss it out and ask for another guess.

cin >> guess[n] is attempting to treat guess as if it were an array, which is isn't at that point. guess is nothing more than an inunitialized string with no length, so trying to index it like that will fail. To fix it just simply remove the index into guess cin >> guess; On the otherhand if you really do want them to enter just a single character then you need to initialize guess to the correct length, such as guess = " "; // 5 spaces then the indexing will work

why do you need to keep all the old guesses? If the guess is wrong then just toss it out and ask for another guess.

cin >> guess[n] is attempting to treat guess as if it were an array, which is isn't at that point. guess is nothing more than an inunitialized string with no length, so trying to index it like that will fail. To fix it just simply remove the index into guess cin >> guess; On the otherhand if you really do want them to enter just a single character then you need to initialize guess to the correct length, such as guess = " "; // 5 spaces then the indexing will work

I need to keep a log of the guess because I need to (at some point down the road) output the letters already guess.

I tried initializing guess with 5 blank spaces but this cause another error on line 102 because I use a string modifier to get the length of the guess string, known as strsz.

I need to be able to read in 6 guesses into the string guess and save each guess as a separate character so i can refer to each individual guess for comparison to the original word to guess

What is odd is that I am able to treat guess

Another way to do it is to use a temp variable for the input

char temp;
cin >> temp;
guess += temp;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.