I can't seem to figure this. For a Hangman game, I have a string for the objective word and I want to get the length of it to make the corresponding underscore version.
I've tried these forms:
1 2 3 4 5 6 7 8 9 10 11 12
// original idea
string strGameWord_ (strGameWord.size(), "_"); // or maybe " _ " for better readability
// tried a static cast
string strGameWord_ (static_cast<int>strGameWord.size(), "_");
// tried an auto instead
auto len = strGameWord.size();
string strGameWord_ (len, " _ "); // really thought this one would work!
I always get this error:
* error: invalid conversion from 'const char*' to 'char' [-fpermissive]|
I've tried putting const on them and different parenthesizing to no avail.
The error is that you're passing a C-string literal (const char*) as the second argument. That ctor wants a single char.
There is no need for the static_cast since size() returns a size_t and the ctor wants a size_t.