-2

I've been trying to get this Hangman using functions (from Michael Dawson's book) program to work, but I have this one error that I don't really understand. I realize my code code could have a variety of bad practices, but please go easy on me as I am a newb. I feel like I am almost there but I'm having trouble figuring out this one error. I am using CodeBlocks. The error is:

32|error: no match for call to '(std::__cxx11::string {aka std::__cxx11::basic_string}) (std::__cxx11::basic_string::size_type, char)'|

//Hangman from Michael Dawson's code
//Uses functions to create the program

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

using namespace std;

//FUNCTION DECLARATION
string pickword();
char playerGuess();
void isitinthere();

char guess = 0;
string soFar = "word";
string used = "";
int wrong = 0;

int main()
{

    const int MAX_WRONG = 8;

    string WORD = pickword();

    soFar = WORD;

    soFar(WORD.size(), '-');
    used = "";

    cout << "Welcome to Hangman! Godspeed!" << endl;

    while ((wrong < MAX_WRONG) && (soFar != WORD))
    {
        cout << "\n\nYou have " << (MAX_WRONG - wrong);
        cout << " incorrect guesses left.\n";
        cout << "\nYou've used the following letters:\n" << used << endl;
        cout << "\nSo far, the word is:\n" << soFar << endl;
    }

    playerGuess();

    while (used.find(guess) != string::npos)
    {
        cout << "\nYou've already guessed " << guess << endl;
        cout << "Enter your guess: ";
        cin >> guess;
        guess = toupper(guess);
    }

    used += guess;

    isitinthere();

if (wrong == MAX_WRONG)
{
    cout << "\nYou've been hanged!";
}
else
{
    cout << "\nYou guessed it!";
}

cout << "\nThe word was " << WORD << endl;

return 0;

}


//FUNCTION DEFINITION

string pickword()
{
    srand(static_cast<unsigned int>(time(0)));

    vector<string> words;

    words.push_back("INDUBITABLY");
    words.push_back("UNDENIABLY");
    words.push_back("CRUSTACEAN");
    words.push_back("RESPONSIBILITY");
    words.push_back("MISDEMEANOR");
    words.push_back("FORENSIC");
    words.push_back("BALLISTIC");
    words.push_back("PARADIGM");
    words.push_back("TROUBARDOR");
    words.push_back("SUPERCALIFRAGILISTICEXPIALLADOCIOUS")

    random_shuffle(words.begin(), words.end());

    theword = words[0];

    return theword;

}

char playerGuess()
{
    cout << "\n\nEnter your guess: ";
    cin >> guess;
    guess = toupper(guess);
    return guess;
}

void isitinthere()
{
    if (WORD.find(guess) != string::npos)
    {
        cout << "That's right! " << guess << " is in the word.\n";

        for (int i = 0; i < WORD.length(); ++i)
        {
            if (WORD[i] == guess)
            {
                soFar[i] = guess;
            }
        }
    }
    else
    {
        cout << "Sorry, " << guess << "isn't in the word. \n";
        ++wrong;
    }
}

Thanks in advance for your help!

sneakily04
  • 11
  • 4
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Jun 23 '17 at 19:06
  • 3
    _`soFar(WORD.size(), '-');`_ Huh?? What do you think that statement should do? – πάντα ῥεῖ Jun 23 '17 at 19:08
  • You're missing out on a few fundamental concepts here that make this question just about impossible to answer in the Q and A format. Fortunately these concepts are covered early in just about any credible C++ text. If you have no text, or an uncredible text, [here is a list of books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) widely accepted as better than credible. – user4581301 Jun 23 '17 at 19:25
  • soFar(WORD.size(), '-'); I was trying to do an exercise out of Michael Dawson's beginner book where you alter his Hangman program to use a few functions. He used that line of code in his book. I thought it had something to do with making each letter of the word a dash so you could see how many letters there are in the word and guess it. The author used this same line to accomplish that and I thought it would still work with my functions. I imagine the problems I am running into have to do with scope; I tried to declare some global variables so my functions could use them. – sneakily04 Jun 24 '17 at 18:37

1 Answers1

0

Here is a simple program that should solve your question.

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

// since you must have function here are some
bool removeGuessFromWord(std::string& word, const char guess);
bool isGuessInWord(const std::string& word, const char guess);
bool hasAlreadyGuessed(const std::vector<char>& gussList, const char guess);

// this is a simple program that should solve your question. It is not optimized for speed or efficency.
int main()
{
    std::vector<std::string> wordList = {"dog","cat","rat"}; // vector of words to select from and use as the word in hangman
    std::vector<char> guessList; // empty vector of gusses
    // Note that I assume a MAX_GUESS_COUNT of 0 means no guesses are allowed
    const unsigned int MAX_GUESS_COUNT = 4U; // number of guesses your allowed

    std::srand(time(0)); // use current time as seed for random generator
    std::string word = wordList.at(std::rand()%wordList.size()); // get a random word in the list
    std::string letersLeft = word; // keep track of what letters will still need to remove

    std::cout << "Welcome to Hangman! Godspeed!" << std::endl;

    char guess = 0;
    for(unsigned int numBadGusses=0U; numBadGusses<MAX_GUESS_COUNT && letersLeft.size()>0U; guess = 0)
    {
        std::cin>>guess;

        if(std::isprint(guess) == 0)
        {
            // may want more error checking
            std::cout << "You ented a non-printable charecter" << std::endl;
        }
        else if(isGuessInWord(word, guess))
        {
            // this was a good guess because the charecter is still in the word
            // so remove all the remaining chars of this type from the word
            if( removeGuessFromWord(letersLeft,guess) )
            {
                std::cout << guess << " was a good guess" << std::endl;
            }
            else
            {
                std::cout << guess << " was a good guess, but you already guessed it once" << std::endl;
            }
        }
        else if(hasAlreadyGuessed(guessList, guess))
        {
            std::cout << "You've already guessed " << guess << std::endl;
        }
        else
        {
            // this was a new bad guess
            guessList.push_back(guess);
            numBadGusses++; // Note that this isn't technicly needed and could use size of vector
            std::cout << guess << " was a bad guess" << std::endl;
        }

    }

    if(letersLeft.size() == 0U)
    {
        std::cout<<"You Win"<<std::endl;
    }
    else
    {
        std::cout<<"You Lose"<<std::endl;
    }
    std::cout << "The word was "<< word << std::endl;

    return 0;
}

bool removeGuessFromWord(std::string& word, const char guess)
{
    return word.erase(std::remove(word.begin(), word.end(), guess), word.end()) != word.end() ? true : false;
}

bool isGuessInWord(const std::string& word, const char guess)
{
    return word.find(guess) != std::string::npos ? true: false;
}
bool hasAlreadyGuessed(const std::vector<char>& gussList, const char guess)
{
    return std::find(gussList.begin(), gussList.end(), guess) != gussList.end() ? true: false;
}
S_P
  • 11
  • 1
  • Thank you for taking the time to respond to my question. I will try out your program, I'm sure I will learn something from analyzing your code. Thanks again. – sneakily04 Jun 24 '17 at 18:42