Well done so far.
Here is a nice tip: check out redhead's reply in the thread below.
http://codenewbie.com/forum/showthre...8610#post18610
Download Dev-Cpp or Code::Blocks for ISO compliance and good IDE and visual debugger. You won't have STL problems anymore. Debugging works easy as well. I think you will like it. I use both myself at work!
So let's move on with your code.
Your function
bool process().
The word "process" could be meaning anything. It's not very informative. Pretend I am a total stranger. And I look for the first time at your code. What could "process" possibly mean to me as a stranger. Could be anything...
Behaviour & Responsibilities
We talked about that earlier. Once you figured out how to do things basically, you may want to think about which part of the program should be doing whatever needs to be done. Your "process()" function does a lot:
- It checks user input for correct guess.
- It offers a supporting role for the gaming engine (it may let you guess again or it may let you give up).
- It even knows what plans the engine has for you.
That's one kick-ass function you got there. It has a lot of responsibilities. We don't want that

.
So I skinned that function down and renamed a few stuff here and there. Remember, to chose the right names for the things so strangers know what you mean. You communicate through code. Never forget that!
Your task
Grab the complete working code from below (at the very end of this post) and wrap this part in some sort of a function. This will be THE game engine. THE controller. THE boss.
Code:
//Start Game
std::cout << "Welcome to Word-Scramble." << std::endl;
std::cout << "Your Word to guess is: " << sShuffledWord << std::endl;
std::string sClientGuess;
//Begin user guessing sequence.
while (1)
{
std::cout << "What is the word? (type \"?\" to get the answer): ";
std::cin >> sClientGuess;
if(sClientGuess == "?")
{
//Client gives up guessing.
std::cout << "The correct word is: " << sWord << std::endl;
break;
}
std::cout << std::endl;
if( check_guess(sClientGuess, sWord) )
{
//User guessed the word so quit guessing sequence.
break;
}
}
When you succeeded you want to check out what else you could do to keep "main()" simple and readable for strangers like me. You want to hide the all the complex things so stranger can concentrate on your global idea first.
Here is the complete code: have fun! (and don't forget to download that stuff)
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);
bool check_guess(const std::string, const std::string);
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...";
std::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);
//Make a copy and shuffle
std::string sShuffledWord = sWord;
std::random_shuffle(sShuffledWord.begin(), sShuffledWord.end());
//Start Game
std::cout << "Welcome to Word-Scramble." << std::endl;
std::cout << "Your Word to guess is: " << sShuffledWord << std::endl;
std::string sClientGuess;
//Begin user guessing sequence.
while (1)
{
std::cout << "What is the word? (type \"?\" to get the answer): ";
std::cin >> sClientGuess;
if(sClientGuess == "?")
{
//Client gives up guessing.
std::cout << "The correct word is: " << sWord << std::endl;
break;
}
std::cout << std::endl;
if( check_guess(sClientGuess, sWord) )
{
//User guessed the word so quit guessing sequence.
break;
}
}
}
else //Incorrect main menu usage.
{
cout << endl << "ERROR: you didn't enter \"1\" or \"2\" " << endl;
cout << "Please try again." << endl << endl;
}
}
std::cout << "Thanks, for playing." << std::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;
}
//-----------------------------------------------------------------------------
bool check_guess(const std::string guess, const std::string word)
{
//Did the user guess right?
if (guess == word)
{
//User Guessed right
std::cout << "Correct! You guessed the word!" << std::endl;
return true;
}
else
{
//User guessed wrong.
std::cout << "No, that\'s not the word." << std::endl;
return false;
}
}