Briana Gardener
Briana Gardener

Reputation: 1

Breaking up long Strings

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!

Upvotes: 0

Views: 152

Answers (1)

Thomas Matthews
Thomas Matthews

Reputation: 57728

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.

Upvotes: 1

Related Questions