Can you please help me?

Started by
4 comments, last by nomichi 18 years, 1 month ago

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;

int main()
{
    vector<string> wlist;
    wlist.push_back("hangman");
    wlist.push_back("chewbaca");
    wlist.push_back("gremlinofdoom");
    wlist.push_back("kitten");
    wlist.push_back("vomit");
    wlist.push_back("optimusprime");
       
const int Max_Wrong = 7;
srand(time(0));
random_shuffle(wlist.begin(), wlist.end());
const string Word_Used = wlist[0];
int wrong = 0;
string So_Far = " ";
string guess;
cout << "Have fun and enjoy my Hangman ripoff.\n\n" << endl;
cout << "Errors: Name gets bigger every time, same word. Cannot use indivisual";
cout << " letters. Must reguess the whole word. Trying to fix..." << endl;


while((wrong < Max_Wrong) && (guess != Word_Used))
    {
        cout << "You still have " << Max_Wrong - wrong << " guesses." << endl;
        cout << "The word so far is: " << So_Far << endl;
        guess = " ";
        cout << "What is your guess? " << endl;
        cin >> guess;
        transform(guess.begin(), guess.end(), guess.begin(), (int(*)(int))tolower);

        for(int i = 0; i < Word_Used.length(); i++)
        {
            if(i < guess.length() && Word_Used == guess)
                So_Far += Word_Used;
            else So_Far += "-"; 
        }

        if (Word_Used == guess)
        {
            cout << "You guessed the word!!!\n" << endl;
            break;
        }
        else
        {
            cout << "Sorry, " << guess << " isn't correct.\n" << endl;
            ++wrong;
        }
    }


if (wrong == Max_Wrong)
    cout << "You got served!!!" << endl;
else
{
    cout << "You guessed it!" << endl;
}    
    cout << "The word was " << Word_Used << endl;
    
    return 0;
}

The problem is this; say answer is hangman and you type a, it desplays ------- and gives you an incorrect, youhave to have the first letter. After you get one, h------, next turn if you guess wrong it shows h------------- and just keeps getting longer. I have no idea how to fix it can you help?
Advertisement
Reset So_Far every loop before appending to it?
I don't have a lot of time to look through your code at this moment but -

Would it be easier to initialize so_far as so:

so_far(Word_Used.length(), "-");


I'd look through the whole thing but I'm at work and my eyes are tired =P
CraniumDrip has the right notion for filling in So_Far, although it's expecting a char, not a const char*.

Secondly, an easy way to make this work is to compare it to So_Far instead of trying to make guess a string itself. By making guess into a char, we can use it to fill in So_Far, if it matches, and then check if So_Far == Word_Used.

If you really get stumped, I can post the solution, but see if you can work it out for yourself first.
Hangman's a neat game to start with. I'm not clear how you are interpreting the rules though. Do you mean to have the player guess the letters in order? This might be a good opportunity to practice function calls. Perhaps you could set up a function where you pass in the search word and the letter that the player is guessing and then have the function return the index of the letter within the word to guess if it is found or -1 if not. That would give you something like this...

idx = checkGuess(Word_Used, guess) ;
if ( idx > 0 )
So_Far[idx] = guess ;


Of course this is just pseudocode and there are better ways to design such a function, but this should at least get the thought process going. You might be having difficulty with your code because everything is bunched up in one big main() instead of separating out functions that do one thing and one thing only.

Food for thought. Good luck.
I recently did a hangman game as well. It uses classes so I don't know if you've covered those yet, but you are welcome to look through the source if you want. At the least you can see how someone else did it.

Goodluck with your version! :)

Download Hangman
Regards,nomichi

This topic is closed to new replies.

Advertisement