samedi 27 novembre 2021

How to copy a character from basic string into a vector string?

//Defining the class

class Hangman
{
    private:
        vector<string> dictionary;          //stores all the words
        vector<string> secretWord;          //stores the secret word
        vector<string> misses;              //keeps record of wrong guesses
        vector<string> displayVector;           //Stores "_"
        string originalWord;                //stores a copy of secret word to display at the 
end of game.
        bool gameOver = false;                      //Flag to check if the player lost or 
still in the game.
        int totalAttempts;
            
    public:                                 
    void selectRandWord();                      
};

//This is the function i am having problem in.

void Hangman::selectRandWord()
{
    secretWord.clear();

//word is a basic string that stores a random word. lets say "Hello World".

    string word;
    srand(time(NULL)); 
    int random = (rand() % dictionary.size()) + 1;

//I store a random word from vector to word.

    word = dictionary[random];
    transform(word.begin(), word.end(), word.begin(), ::tolower);           
    originalWord = word;
    for (int index = 0; index < word.length(); index++) 
    { 

//This line has the error: [Error] invalid user-defined conversion from 'char' to 'std::vectorstd::basic_string<char >::value_type&& {aka std::basic_string&&}' [-fpermissive]

//What I am trying to do is take each character from word(for example: "H") and push it back into the vector string secretWord.

        secretWord.push_back(word[index]); 
    } 
}

Aucun commentaire:

Enregistrer un commentaire