Sign in to follow this  
NUCLEAR RABBIT

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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(), '-');.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this