Using string::size_type to direct init. another string

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.

Any ideas?
Last edited on
You need to use single quotes around the underscore character or else it is a string, not a character.
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.
Last edited on
Oh my oh my! How did I forget to use single quotes? Thanks guys! :)
Last edited on
Topic archived. No new replies allowed.