In Michael Dawson's book, " Beginning C++ Game Programming ", there was and exercise saying to rewrite a hangman game learned earlier in the book using functions. I tried all i could to get it run correctly, but i got nowhere. Can someone tell me what I am doing wrong?
I'm a little new to programming, so what i did probably looks dumb. Haha.
well here it is....
Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;
const int MAX_WRONG = 8;
int WRONG;
string Guess(string soFar, string used);
string check_guess(string guess, string used, string soFar, string THE_WORD);
int main()
{
vector<string> words;
words.push_back("AMAZING");
words.push_back("DIFFICULT");
words.push_back("COMPATABLE");
srand(time(0));
random_shuffle(words.begin(), words.end());
const string THE_WORD = words[0];
WRONG = 0;
string soFAR(THE_WORD.size(), '-');
string used = "";
cout << "Welcome to Hangman. Good luck!\n";
while ((WRONG < MAX_WRONG) && (soFAR != THE_WORD))
{
string guess(string soFar, string used);
string check_guess(string guess, string used);
}
if (WRONG == MAX_WRONG)
{
cout << "\nYou've been hanged!";
}
else
{
cout << "\nYou guessed it!";
}
cout << "\nThe word was " << THE_WORD << endl;
return 0;
}
string Guess(string soFar, string used)
{
cout << "\n\nYou have " << (MAX_WRONG - WRONG) << " incorrect guesses left.";
cout << "\nYou've used the following letters:\n" << used << endl;
cout << "\nSo far, the word is:\n" << soFar << endl;
char guess;
cout << "\n\nEnter your guess: ";
cin >> guess;
guess = toupper(guess);
while (used.find(guess) != string::npos)
{
cout << "\nYou've already guessed ";
cout << "Enter your guess: ";
cin >> guess;
guess = toupper (guess);
}
used += guess;
return guess, used;
}
string check_guess(string guess, string used, string soFar, string THE_WORD)
{
if (THE_WORD.find(guess) != string::npos)
{
cout << "That's right! " << guess << " is in the word.";
for (int i = 0; i < THE_WORD.length(); ++i)
{
cout << THE_WORD[i];
}
}
else
{
cout << "Sorry, " << guess << " isn't in the word.";
++WRONG;
}
return guess;
}
All help appreciated, thank you.