Jump to content
  • Advertisement
Sign in to follow this  
NUCLEAR RABBIT

C++ Vector Help.

This topic is 4116 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Try this instead:

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

This should fix the problem[smile]

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!