0

Cheers! I'm completely new to this coding community--but I'm quite enjoying it thus far. If I have a string, and the cout is multiple lines long, how do I break up these lines?

For example:

string famousVillains; 
famousVillains = 
"
Voldemort: skinny man with a fat backstory 
Ursula: fat lady with tentacles
The Joker: scary dude with make-up 
Cruella:  weird lady with dog obsession
Terminator: crazy guy in black."; 

when I cout this, how do I make sure there are spaces in between each of the villains? I tried using << endl; but this just cancels all of the villains that follow.

Thanks for the help!

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Please try to use proper formatting, show the code you tried, and give *exact* desired output, not handwaving. It will be much easier. – luk32 Oct 21 '14 at 22:06
  • When you say "spaces", do you mean newlines? – ooga Oct 21 '14 at 22:07
  • 3
    possible duplicate of [C++ multiline string literal](http://stackoverflow.com/questions/1135841/c-multiline-string-literal). But what you probably *really* want is to structure your data; so `class Character {private: string name; string description; public: /* methods here */};` and then use a `vector famousVillains;`. That gives you a lot more options for processing characters, counting them, adding and removing them...and varying ways of displaying them. Storing all your data as a single lump string is much more arduous to process. – HostileFork says dont trust SE Oct 21 '14 at 22:10

1 Answers1

1

You can create a std::map or have a table lookup of villains and their descriptions.

struct Villain_Descriptions
{
  std::string name;
  std::string descripton;
};

Villain_Descriptions famous_villains[] =
{
  {"Voldemort", "skinny man with a fat backstory"}, 
  {"Ursula",    "fat lady with tentacles"},
  {"The Joker", "scary dude with make-up"},
  {"Cruella",   "weird lady with dog obsession"},
  {"Terminator", "crazy guy in black."},
};

The lookup table and std::map structures allow you to get information of a villain by searching for their name.
In your method, you have to search a string for newlines or the name, then extract the substring.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154