Hello fellow programmers! I have an exercise to make: I need to rewrite the Hangman game i have written here, but with functions. I need to include a function to get the player’s guess and another function to determine whether the player’s guess is in the secret word. I have done the first function but i am having problems with the other one. It doesn't compile.
// Hangman
// The classic game of hangman
#include <iostream>
#include <string>
#include <ctime>
#include <vector>
#include <algorithm>
#include <cctype>
usingnamespace std;
int main()
{
//setup
constint MAX_WRONG = 8; // maximum number of incorrect guesses allowed
vector<string> words; // collection of possible words to guess
words.push_back("GUESS");
words.push_back("HANGMAN");
words.push_back("DIFFICULT");
srand(time(0));
random_shuffle(words.begin(), words.end());
const string THE_WORD = words[0]; // word to guess
int wrong; // number of incorrect guesses
string sofar(THE_WORD.size(), '-'); // word guessed so far
string used = ""; // letters already guessed;
cout<<"\nWelcome to hangman. Good luck!\n";
// main loop
while ((wrong < MAX_WRONG) && (sofar != THE_WORD))
{
cout<<"\n\nYou have "<<(MAX_WRONG - wrong)<<" incorrect guesses left.\n";
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); // make uppercase since secret word is uppercase
while (used.find(guess) != string::npos)
{
cout<<"\nYou have already guessed "<<guess<<endl;
cout<<"Enter your guess: ";
cin>>guess;
guess = toupper(guess);
}
used += guess;
if (THE_WORD.find(guess) != string::npos)
{
cout<<"That's right! "<<guess<<" is in the word.\n";
// update sofar to include newly guessed letter
for (int i=0; i < THE_WORD.length(); ++i)
if (THE_WORD[i] == guess)
sofar[i] = guess;
}
else
{
cout<<"Sorry "<<guess<<" isn't in the word.\n";
++wrong;
}
}
// shut down
if (wrong == MAX_WRONG)
cout<<"\nYou've been hanged!";
else
cout<<"\nYou guessed it!";
cout<<"The word was "<<THE_WORD<<endl;
system ("PAUSE");
return 0;
}
// Hangman - with functions
#include <iostream>
#include <string>
#include <ctime>
#include <vector>
#include <algorithm>
#include <cctype>
usingnamespace std;
char getguess(string prompt);
void yesorno();
int main()
{
// setup
constint MAX_WRONG = 8; // maximum number of incorrect guesses allowed
vector<string> words; // collection of possible words to guess
words.push_back("GUESS");
words.push_back("HANGMAN");
words.push_back("DIFFICULT");
srand(time(0));
random_shuffle(words.begin(), words.end());
const string THE_WORD = words[0]; // word to guess
int wrong; // number of incorrect guesses
string sofar(THE_WORD.size(), '-'); // words guessed so far
string used = ""; // letters already guessed
cout<<"Welcome to Hangman. Good luck!\n";
// main loop
while ((wrong < MAX_WRONG) && (sofar != THE_WORD))
{
cout<<"\n\nYou have "<<(MAX_WRONG - wrong)<<" incorrect guesses left.\n";
cout<<"\nYou've used the following letters:\n"<<used<<endl;
cout<<"\nSo far the word is:\n"<<sofar<<endl;
char guess;
guess = getguess("Enter your guess: ");
guess = toupper(guess);
while (used.find(guess) != string::npos)
{
cout<<"You have already guessed "<<guess<<endl;
guess = getguess("Enter your guess: ");
guess = toupper(guess);
}
used += guess;
yesorno();
}
// shut down
if (wrong == MAX_WRONG)
cout<<"\nYou have been hanged!";
else
cout<<"\nYou guessed it!";
cout<<"The word was "<<THE_WORD<<endl;
return 0;
}
char getguess(string prompt)
{
char character;
cout<<prompt;
cin>>character;
return character;
}
void yesorno()
{
if (THE_WORD.find(guess) != string::npos)
{
cout<<"That is right! "<<guess<<" is the word.\n";
//update sofar to include newly guessed letter
for (int i=0; i<THE_WORD.length(); ++i)
if (THE_WORD[i] == guess)
sofar[i] = guess;
}
else
{
cout<<"Sorry "<<guess<<" isn't in the word.\n";
++wrong;
}
}
@Mats OK i think i fixed it and it compiles without errors, but another problem showed up now, it just ignores the main loop and tells the answer directly. Any ideas?
// Hangman - Rewrited with functions
// End of chapter - Functions
#include <iostream>
#include <string>
#include <ctime>
#include <vector>
#include <algorithm>
#include <cctype>
usingnamespace std;
void prompt();
char getguess(char& a);
void yesorno(const string& a, char& b, string& c, int& d);
int main()
{
// setup
constint MAX_WRONG = 8; // maximum number of incorrect guesses allowed
vector<string> words; // collection of possible words to guess
words.push_back("GUESS");
words.push_back("HANGMAN");
words.push_back("DIFFICULT");
srand(time(0));
random_shuffle(words.begin(), words.end());
const string THE_WORD = words[0]; // word to guess
int wrong; // number of incorrect guesses
string sofar(THE_WORD.size(), '-'); // words guessed so far
string used = ""; // letters already guessed
cout<<"Welcome to Hangman. Good luck!\n";
// main loop
while ((wrong < MAX_WRONG) && (sofar != THE_WORD))
{
cout<<"\n\nYou have "<<(MAX_WRONG - wrong)<<" incorrect guesses left.\n";
cout<<"\nYou've used the following letters:\n"<<used<<endl;
cout<<"\nSo far the word is:\n"<<sofar<<endl;
char guess;
prompt();
getguess(guess);
guess = toupper(guess);
while (used.find(guess) != string::npos)
{
cout<<"You have already guessed "<<guess<<endl;
prompt();
getguess(guess);
guess = toupper(guess);
}
used += guess;
yesorno(THE_WORD, guess, sofar, wrong);
}
// shut down
if (wrong == MAX_WRONG)
cout<<"\nYou have been hanged!";
else
cout<<"\nYou guessed it!";
cout<<"The word was "<<THE_WORD<<endl;
return 0;
}
void prompt()
{
cout<<"Enter your guess: ";
}
char getguess(char& a)
{
cin>>a;
return a;
}
void yesorno(const string& a, char& b, string& c, int& d)
{
if (a.find(b) != string::npos)
{
cout<<"That't right "<<b<<" is in the word.\n";
// update sofar to include newly guessed letter.
for (int i=0; i<a.length(); ++i)
if (a[i] == b)
c[i] = b;
}
else
{
cout<<"Sorry "<<b<< " isn't in the word.\n";
++d;
}
}