The program below compiles and runs the way i need it to. However, part of the assignment was to load words, MAX_WORDS=100, into the program and then randomly have the program pick the word. Im really struggling with this part. The hint was to use a LoadWord () function but i cant seem to work it properly into the program. Please help.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

string THE_WORD; // word to guess
int wrong;
string soFar;
string used;

bool match(char letter, string word);
char askGuess(string usedLettersStr); 
bool playAgain();
void DrawGallows (int wrong, int MAX_WRONG);

int main()
{
	srand(time(0)); //random selection of words in the list

	vector<string> words; // collection of possible words to guess

	words.push_back("APPLE");
	words.push_back("BABY");
	words.push_back("BINARY");
	words.push_back("BIRD");
	words.push_back("BLUEBERRY");
	words.push_back("BREAKFAST");
	words.push_back("BROTHER");
	words.push_back("CAFETERIA");
	words.push_back("CALIFORNIA");
	words.push_back("CANTALOUPE");
	words.push_back("CAT");
	words.push_back("CHICKEN");

	cout << " Welcome to Hangman! \n";

	bool done = false;
	do
	{
		const int MAX_WRONG = 7; // maximum number of incorrect guesses allowed

		random_shuffle(words.begin(), words.end());
		THE_WORD = words[0]; // word to guess

		soFar = string(THE_WORD.size(), 'X'); // word guessed so far
		used = ""; // letters already guessed

		// loop for current word
		while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
		{
			DrawGallows ( wrong, MAX_WRONG);
			cout << "\n\nYou have " << (MAX_WRONG - wrong)
				<< " incorrect guesses left.\n";
			cout << "\nYou've picked the following correct letters:\n" << used
				<< endl;
			cout << "\nSo far, the word is:\n" << soFar << endl;

			used += askGuess(used);

			{

			} // end of while ((wrong < MAX_WRONG) && (soFar != THE_WORD))

			// shut down


		}

		cout << "\nThe word was " << THE_WORD << endl;

	} while (playAgain());

	return 0;
}

inline bool match(char letter, string word)
{
	return (word.find(letter) != string::npos);
}

char askGuess(string usedLettersStr)
{
	char guess;
	cout << "\n\nEnter your guess: ";
	cin >> guess;
	guess = toupper(guess); //make uppercase since secret word in uppercase
	while (used.find(guess) != string::npos)
		while (match(guess, used))
		{
			cout << "\nYou've already guessed " << guess << endl;
			cout << "Enter your guess: ";
			cin >> guess;
			guess = toupper(guess);
		}

	// if (THE_WORD.find(guess) != string::npos)
	if (match(guess, THE_WORD))
	{
		cout << "That's right! " << guess << " is in the word.\n";

	}
	else
	{
		cout << "Sorry, " << guess << " isn't in the word.\n";
		++wrong;
	}

	// update soFar to include newly guessed letter
	for (unsigned int i = 0; i < THE_WORD.length(); ++i)
		if (THE_WORD[i] == guess)
			soFar[i] = guess;

	return 0;
}

bool playAgain() // function to play again while clearing system
{
	char again;
	cout << "\n\nWould you like to play again? <y/n>: ";
	cin >> again;

	cin.clear(); //clear and ignore cin
	cin.ignore();

	again = toupper(again);

	system("cls");

	return (again == 'Y');
}

void DrawGallows(int wrong, int MAX_WRONG)
{
 if(wrong==MAX_WRONG) // The User is Dead
 {
  
  cout<<endl<<endl
   <<"      O     "<<endl
   <<"            "<<endl
   <<"     /|\    "<<endl
   <<"      |     "<<endl
   <<"     / \    "<<endl
   <<"    /   \   "<<endl
   <<"YOU ARE DEAD"<<endl<<endl;
 }
 else if(wrong == 6) // Close to being dead
 {
  cout<<endl<<endl
   <<"      O     "<<endl
   <<"      |     "<<endl
   <<"     /|\    "<<endl
   <<"      |     "<<endl
   <<"     / \    "<<endl
   <<"    /   \   "<<endl
   <<"            "<<endl<<endl;

 }
 else if(wrong == 5) // The user has more attempts
 {
  cout<<endl<<endl
   <<"      O     "<<endl
   <<"      |     "<<endl
   <<"     /|\    "<<endl
   <<"      |     "<<endl
   <<"     / \    "<<endl
   <<"            "<<endl
   <<"            "<<endl<<endl;
 }
 else if(wrong == 4) // The user has more attempts
 {
  cout<<endl<<endl
   <<"      O     "<<endl
   <<"      |     "<<endl
   <<"     /|\    "<<endl
   <<"      |     "<<endl
   <<"            "<<endl
   <<"            "<<endl
   <<"            "<<endl<<endl;
 }
 else if(wrong == 3) // The user has more attempts
 {
  cout<<endl<<endl
   <<"      O     "<<endl
   <<"      |     "<<endl
   <<"     /|\    "<<endl
   <<"            "<<endl
   <<"            "<<endl
   <<"            "<<endl
   <<"            "<<endl<<endl;
 }
 else if(wrong == 2) // Begining of the game
 {
  cout<<endl<<endl
   <<"      O     "<<endl
   <<"      |     "<<endl
   <<"            "<<endl
   <<"            "<<endl
   <<"            "<<endl
   <<"            "<<endl
   <<"            "<<endl<<endl;
 }
 else if (wrong == 1)
 {
  cout<<endl<<endl
   <<"      O     "<<endl
   <<"            "<<endl
   <<"            "<<endl
   <<"            "<<endl
   <<"            "<<endl
   <<"            "<<endl
   <<"            "<<endl<<endl;
 }

}

Recommended Answers

All 2 Replies

In general, you should post only code relevant to the question you are asking. For example, here you basically just want to know how to read words from a file, and that has nothing to do with hangman.

This is how I would do it:

std::ifstream fin(Filename.c_str());

  if(fin == NULL)
      std::cout << "Cannot open file." << std::endl;

  std::vector<std::string> Lines;
  std::string line;
  
  while(getline(fin, line))
  {
      Lines.push_back(line);
  }
	
  for(unsigned int i = 0; i < Lines.size(); i++)
  {
    std::cout << Lines[i] << std::endl;
  }

Dave

Thanks got it figured out.

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.