Don't do this "part" method. Use a simple global game loop. Also observe the usage of std::random_shuffle. Makes your life quite easy. After you've seen the code things are easy for you to do implement the rest:
Code:
#include <fstream>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <algorithm>
unsigned choose_word(unsigned);
unsigned calculate_lines(std::ifstream&);
std::string fetch_word(std::ifstream&, unsigned);
int main()
{
using std::endl;
using std::cout;
using std::cin;
std::string sWord;
std::string sGuess = "quit";
unsigned nLines(0);
std::ifstream dictionary("dictionary.txt");
if (!dictionary)
{
std::cout << "ERROR: There was error opening the file dictionary.txt." << std::endl;
std::cout<< "Please read readme.txt for possible solutions." << std::endl;
std::cout<< "Terminating program...";
exit(1);
}
while(1)
{
cout << "1 = Play Game" << endl;
cout << "2 = Quit App" << endl;
char appRun;
cin >> appRun;
if(appRun == '2')
{
break;
}
if(appRun == '1')
{
//Let's first determine how many words there are.
nLines = calculate_lines(dictionary);
//We know how many words there are, so let's choose a random word line.
unsigned RandomLine;
RandomLine = choose_word(nLines);
//We know which line to pick. So let's retreive the word on that line.
sWord = fetch_word(dictionary, RandomLine);
std::random_shuffle(sWord.begin(), sWord.end());
}
else
{
cout << endl << "ERROR: you didn't enter \"1\" or \"2\" " << endl;
cout << "Please try again." << endl << endl;
}
}
return 0;
}
//---------------------------------------------------
//So how many lines (=words) has this file?
unsigned calculate_lines(std::ifstream& dict)
{
unsigned lns(0);
std::string sBuffer;
while( std::getline(dict, sBuffer) )
{
++lns;
}
//Did something went wrong during the read of this stream?
if(!dict.eof())
{
//Yep, something went wront. Ring the alarms!
std::cout<<"ERROR: something go wrong during file reading.";
dict.close();
std::cout<<"exiting app...";
}
//Do not forget to reset the state of the dictionary file object since it is
//in a error state due to EOF.
dict.clear();
//And let the file pointer point to the beginning as it was before all this.
dict.seekg(0, std::ios::beg);
return lns;
}
//---------------------------------------------------
unsigned choose_word(unsigned maxnum)
{
unsigned randnum;
std::srand(std::time(0));
randnum= std::rand() % maxnum + 1;
return randnum;
}
//----------------------------------------------------
std::string fetch_word(std::ifstream& dict, unsigned line)
{
std::string buffer;
while( std::getline(dict, buffer) && line != 1)
{
--line;
}
//Do not forget to reset the state of the dictionary file object since it is
//in a error state due to EOF.
dict.clear();
//And let the file pointer point to the beginning as it was before all this.
dict.seekg(0, std::ios::beg);
return buffer;
}
Soo... what do you think?