Sign in to follow this  
kevtimc

String function error

Recommended Posts

kevtimc    230
// Hangman
// The classic game of hangman

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

char GetGuess();

void IsGuessInWord(char guess, string THE_WORD, string soFar, int wrong);

int main()
{
    // set-up
    const int 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());           // word to guess
    const string THE_WORD = words[0];
    int wrong = 0;                               // number of incorrect guesses
    string soFar(THE_WORD.size(), '-');          // word 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();
        while (used.find(guess) != string::npos)
        {
            cout << "\nYou've already guessed " << guess << endl;
            cout << "Enter your guess: ";
            cin >> guess;
            guess = toupper(guess);
        }

        used += guess;

IsGuessInWord(guess, THE_WORD, soFar, wrong);

    }

    // shut down
    if (wrong == MAX_WRONG)
        cout << "\nYou've been hanged!";
    else
        cout << "\nYou guessed it!";
    
    cout << "\nThe word was " << THE_WORD << endl;

    return 0;
}

char GetGuess() 
{
char cGuess;
        
cout << "\n\nEnter your guess: ";
cin >> cGuess;
cGuess = toupper(cGuess); //make uppercase since secret word in uppercase

return cGuess;
}

void IsGuessInWord(char guess, string THE_WORD, string soFar, int wrong)
{
             if (THE_WORD.find(guess, THE_WORD) != string::npos) //ERROR HERE!
        {
            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;
        
        }
}


Hey, first off all lemme say this is not homework, it's from a book. So anyways, I'm supposed to spilt this hangman game into two functions, and Ive got it down to one more compiler error. It a long error message (83 E:\Documents and Settings\Kev\Desktop\Book Exercizes\Hangmanwfunctionsprg.cpp no matching function for call to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::find(char&, std::string&)' ) and it seems like its something wrong with the string functions. Can canyone shed some light? (ERROR marked in code!)

Share this post


Link to post
Share on other sites
rip-off    10979
Quote:
Original post by kevtimc
Does it have to do with the comma? Am I comparing the char and string wrong?


Lets see:


used.find(guess) != string::npos // works

THE_WORD.find(guess, THE_WORD ) != string::npos // has error


Any better? [smile]

Share this post


Link to post
Share on other sites
kevtimc    230
There is no more complie error when you take , THE_WORD out, but it has a run time error, if you guess the complete word, it dosent work properly.

Share this post


Link to post
Share on other sites
pulpfist    528
There is no need to provide THE_WORD as an agrument to THE_WORD's find operation ^^
There is two types of std::string find. Each has different signature.
The way you use the find function dont match with any of them

Share this post


Link to post
Share on other sites
kevtimc    230
Ah! nvm I see what went wrong, I manually incuded THE_WORD in the argument by accident, I must have been thinking I was changing the function arguments. Thnx! :)

Share this post


Link to post
Share on other sites
pulpfist    528
One thing you should be aware of is that when you send soFar as an argument to the IsGuessInWord you send a copy of soFar.

The current signature of IsGuessInWord:

void IsGuessInWord(char guess, string THE_WORD, string soFar, int wrong)

We say that soFar is passed by value
When the IsGuessInWord returns, soFar is unchanged and the copy of soFar is deleted.
The common solution to this problem is to send soFar by reference
The new signature then looks like:

void IsGuessInWord(char guess, string THE_WORD, string& soFar, int wrong)
(Note the ampersand in front od soFar)

Now, the changes to soFar in the IsGuessInWord will indeed affect the original

Share this post


Link to post
Share on other sites
kevtimc    230
Gotcha, the book didn't focus on references yet, (next chapter), but ive read about them so I know how it works.

Share this post


Link to post
Share on other sites
Sutekh    122
You could also pass THE_WORD by reference, copying a string or any large structure is usually a bad idea, to quicken things up, efficiency and such :). Remember to make anything you pass that isn’t going to be altered in the function a const tho.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this