vendredi 23 juin 2017

Hangman w/ Functions - Compile Error - No Match for Call To

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!

Aucun commentaire:

Enregistrer un commentaire