creating functions problem
Jul 4, 2011 at 11:41am UTC
hi guys. i tried to make the classic hangman game but not entirely builded in int main()! i tried to create 2 functions one for the player's guess and one for checking if the player's guess is in the secret word. but i obviously failed:/ so
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <ctime>
#include <cctype>
void guessing();
void checking();
using namespace std;
vector<string> words(3);
const string The_Word = words[0];
string soFar(The_Word.length(), '-' );
const int MAX_wrong = 8;
unsigned int wrong = 0;
string used = " " ;
char guess;
int main()
{
vector<string> words(3);
words.push_back("RAGE" );
words.push_back("GAMATILA" );
words.push_back("STELEXOS" );
srand((unsigned int )time(0));
random_shuffle(words.begin(), words.end());
cout << "Welcome to Hangman. Good luck! \n" ;
guessing();
checking();
if (wrong == MAX_wrong)
{
cout << "You've been hanged!" ;
}
else
{
cout <<"You guessed it!" ;
cout <<"The word was " << The_Word << endl;
return 0;
}
}
void guessing()
{
while ((wrong < MAX_wrong) && ( soFar != The_Word))
{
cout << "You have " << (MAX_wrong - wrong) << "incorrect guesses left." << endl;
cout << "You've used the following letter: " << endl << used << endl;
cout << "So far, the word is " << soFar << endl;
char guess;
cout << "Enter your guess: " << endl;
cin >> guess;
guess = toupper(guess);
}
}
void checking()
{
while (used.find(guess) != string::npos)
{
cout << "You've allready guess " << 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." << endl;
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." << endl;
++wrong;
}
}
there is no syntax problem during compile or any error is just that it doesnt really enters the loop at all..
Jul 4, 2011 at 12:16pm UTC
Does it not even output the first cout line of guessing()?
cout << "You have " << (MAX_wrong - wrong) << "incorrect guesses left." << endl;
Jul 4, 2011 at 12:23pm UTC
Jul 4, 2011 at 12:54pm UTC
For some reason it skips your while loop from guessing(), you can try to put some cout<< statements just before the while loop and see if your variables are what they're supposed to be.
Jul 4, 2011 at 1:14pm UTC
still cant figure this out.
Jul 4, 2011 at 1:14pm UTC
while ((wrong < MAX_wrong) && ( soFar != The_Word))
soFar is identical to The_Word at this point.
Last edited on Jul 4, 2011 at 1:15pm UTC
Jul 4, 2011 at 1:21pm UTC
Moschops is right,
1 2
const string The_Word = words[0];
string soFar(The_Word.length(), '-' );
You mean to assign a word to The_Word in the first line, but the vector words isn't initialized yet, so it's filled with an empty string. Then you initialize soFar, but it gets parameter (0, '-') because The_Word is an empty string.
Topic archived. No new replies allowed.