mitee 0 Newbie Poster

Hi, I am new c++ programming and I need some help with my code.

I am supposed to create a Hangman game with a file and your supposed to be able to write new words to the file etc...

But I have problems with the code after the user have played two times the program shut downs. And I am also supposed to skip over empty lines and I have tried several things with the getline in the code but it just fails all the time. I am getting really frustrated with the code and any help would be very helpful.

// This will be a game of hangman
/*Komplettera, att man inte behöver ha tomt ord att gissa på. Se line 90. I main så skriver du över filen med ord hela tiden se line 37.*/
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <climits>
#include <map>
#include <iomanip>
#include <vector>
using namespace std;

typedef map<string, int>& HangmanMap;
typedef map<string, int>::iterator HangmanMapIterator;
const int NUM = 20;
const char *hang = "Hangman.txt";
ofstream fout;
ifstream fin;

enum MenuChoices
{
	AddNewWord = 1,
	PlayaNewSession,
};

int menu ();
void read (HangmanMap hangman);
void write(HangmanMap hangman);


int main()
{
	/*ofstream fout(hang);
	//fout.open(hang);
	fout << "google\njeans\njump\ndad\nmother\nsorry\nape\nreluctant\nnews\npapper\nbad\nhappy\n";
	fout.close ();*/ // Removed because the word is supposed to be saved after every game

	// Will begin with Menu
	map<string, int> hangman;

	int menuChoice = 0;

	while ((menuChoice = menu()) > 0)
	{
	switch (menuChoice) 
	{
	case AddNewWord:
		write(hangman);
			break;
	case PlayaNewSession:
		read(hangman);
		break;
	}

	};
	
	
	return 0;

}


int menu()

{
int choice = 0;

	cout << setfill('/') << std::setw(9) << "/\n";
	cout << AddNewWord << ".\t Enter new word \n";
	cout << PlayaNewSession << ".\t Play a session \n";
	cout << "0.\t Quit \n";
	cin >> choice;

	return choice;
}


void read(HangmanMap hangman)
{

	ifstream fin(hang);

	vector<string> lines;
	string line;

	while (getline(fin, line))
	{
		
		if (line.find("")){continue;} //Om det kommer upp ett tomt ord så istället för att gissa på det, så fortsätter loopen.
		lines.push_back(line);
	}
	
	string target = lines[std::rand() % NUM];
	int length = target.length();
	string guess(length, '_');
	string wrongGuess;
	int guesses = 6;
	std::cout << "Guess my word it has " << length << " letters\n" << "and you will get " << guesses <<  " guesses to guess wrong\n";
	std::cout << "Your word: " << guess << endl;

	while (guesses > 0 && guess != target)
	{
	char letter;
	std::cout << "Please enter a letter: ";
	std::cin >> letter;

	if(wrongGuess.find(letter) != string::npos || guess.find (letter) != string::npos)
	{
	cout << "That letter has been used please try again \n";
		continue;
	}
	int lok = target.find(letter);
	if (lok == string::npos)
	{
	cout << "Iam sorry that was a wrong guess\n";
	--guesses;
	wrongGuess += letter;
	
	}
	else
	{
	cout << "Thats a letter\n";
	guess[lok] = letter;
	lok = target.find(letter, lok + 1);
	while (lok != string::npos)
	{
	guess[lok] = letter;
	lok = target.find(letter, lok + 1);
	}
	
	}
	std::cout << "your word: " << guess << endl;
	if (guess != target)
	{
	if (wrongGuess.length() > 0)
		cout << "Wrong guess: " << wrongGuess << endl;
	cout << guesses << " wrong guesses left. \n"; 
	
	}
	}
	if (guesses > 0)
		cout << "That was correct\n";
	else 
		cout << "thats wrong, the word is " << target << ".\n";

	return;


}

void write (HangmanMap hangman)

{

	ifstream fin;

	fin.open(hang);
	
	char ch;
	
	
	if(fin.is_open())
	{
	std::cout << "this is the current info in the file\n" <<hang<< endl;
	while (fin.get(ch))
		cout << ch;
	fin.close();
	}
	

	ofstream fout(hang, ios_base::out | ios_base::app);
	if (!fout.is_open())
	{
	std::cout << "cant open file\n";
		exit(EXIT_FAILURE);
	}

		std::cout <<"Please enter a word to be included: \n";
		string word;
			while (getline(cin, word))
			{
				if(word.size() > 0)
				{
				fout << word << endl;
				
				break;}
			}
			fout.close();


			fin.clear();
			fin.open(hang);
			if(fin.is_open())
			{
			std::cout << "Here is the current words, \nbut don't cheat and check the words while playing:)!\n" << hang << endl;
			while (fin.get(ch))
				cout << ch;
			fin.close();
			}
		
				return;
				

}