C++ Vector Help.

Started by
5 comments, last by Zahlman 17 years, 1 month ago
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 [:(]
Advertisement
Try this instead:
    std::vector<std::string> soFar(THE_WORD.size(), "-");

This should fix the problem[smile]
I think you want a vector of char's instead of a vector of strings.
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.

[size=1]Visit my website, rawrrawr.com

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(), "-");
[size=2]Darwinbots - [size=2]Artificial life simulation
thanks for the quick replies and all the help everyone [:D]

I forgot that I needed to use double quotes.
Quote:Original post by Numsgil
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.


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(), '-');.

This topic is closed to new replies.

Advertisement