# C++ Vector Help.

## Recommended Posts

Hello, I need help with a little bit of code. I'm trying to make a vector(string) be the same length of a string, but all the letters of the vector be '-'. this is what I have:
    std::string THE_WORD = "POOP";

std::vector<std::string> soFar(THE_WORD.size(), '-');


when I try this, i get the error: C:\Documents and Settings\brandon\My Documents\C++ Projects\Hangman\main.cpp invalid conversion from char' to const char*' Please help me out! Im rusty from a month break [:(]

##### Share on other sites
Crypter    748
    std::vector<std::string> soFar(THE_WORD.size(), "-");

This should fix the problem[smile]

##### Share on other sites
Amnesty2    203
I think you want a vector of char's instead of a vector of strings.

##### Share on other sites
bobofjoe    322
What your doing is giving a char to a vector that expects a std::string or a character array (Which will be converted to an std::string)
To fix this, just add double quotes instead of single quotes ( std::vector<std::string> soFar(THE_WORD.size(), "-"); ) it should work then.

EDIT:: Drat! Too late.

##### Share on other sites
Numsgil    501
I don't think you want a vector of strings. What's the purpose you're going after? I'll bet you can just use either a string or a vector of chars, and get the effect you want.

To the error, you're trying to pass a single character when std::strings want arrays of characters. Hence your difficult.

Try:
std::vector<std::string> soFar(THE_WORD.size(), "-");

##### Share on other sites
thanks for the quick replies and all the help everyone [:D]

I forgot that I needed to use double quotes.

##### Share on other sites
Zahlman    1682
Quote:
 Original post by NumsgilI don't think you want a vector of strings. What's the purpose you're going after? I'll bet you can just use either a string or a vector of chars, and get the effect you want.

Quoted for emphasis.

/me puts on mindreading hat. Are you making Hangman by any chance?

Why not just make a string of that length out of dashes? (The constructor call would be similar, and you can index into a std::string the same way you do into a vector). Failing that, any time you make a container-of-something, you should make sure you know what type 'something' should be, and why. Here, it looks like you want a vector of char, because each element is supposed to be a char (to start out with, a hyphen).

That would be std::vector<char> soFar(theWord.size(), '-');.