Jessica
Jessica

Reputation: 119

Can you put breaks in a string literal?

This is a constant I am using, it is split like this because I did not want to have to scroll my editor

const string PROGRAM_DESCRIPTION = "Program will calculate the amount "
"accumulated every month you save, until you reach your goal.";

int main() 
{
    cout << PROGRAM_DESCRIPTION;
    return 0;
}

Currently prints out in command prompt as

Program will calculate the amount accumulated every month you save,until you re
ach your goal.

When this prints out I would like it to print on two separate lines like below...

Program will calculate the amount accumulated every month you save,
until you reach your goal.

I do not know where to put break statements in a string so I can get it to print out correctly.

Upvotes: 0

Views: 379

Answers (6)

ST3
ST3

Reputation: 8946

Well answers are good, however my offer is to use \r\n for new line. Read here some more, these two symbols should always work (unless you are using Atari 8-bit OS).

And some more explanation.

  • \n - Line Feed. This should move printing pointer one line lower, but it may or may not set printing pointer to beginning o line.
  • \r - Carriage Return. This sets line pointer at the beginning o line, and may or may not change line.

  • \r\n - CR LF. Moves printing pointer to next line and sets it at the beginning of line.

Upvotes: 0

Hugo Oshiro
Hugo Oshiro

Reputation: 380

Just insert a line break "\n" in the position you want in your const string, as you would do in a normal literal string:

const string PROGRAM_DESCRIPTION = "Program will calculate the amount\naccumulated every month you save, until you reach your goal.";

cout << PROGRAM_DESCRIPTION;

Simple. Just the same way as you should me used to in using a literal string:

cout << "Program will calculate the amount\naccumulated every month you save, until you reach your goal.";

Right?

Upvotes: 0

IdeaHat
IdeaHat

Reputation: 7881

In C++11, you can use a raw string literal

const char* stuff =
R"foo(this string
is for real)foo";
std::cout << stuff;

outputs:

this string
is for real

(I put this answer here for pedantic reasons, use \n)

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

You can use \n at the end of the first part of the literal for that, like this:

const string PROGRAM_DESCRIPTION = "Program will calculate the amount\n"
"accumulated every month you save, until you reach your goal."; //   ^^

You do not have to split the literal into parts if you do not wish to do so for readability:

const string PROGRAM_DESCRIPTION = "Program will calculate the amount accumulated every month you save,\nuntil you reach your goal.";

Upvotes: 4

Trenin
Trenin

Reputation: 2063

Add \n in the string where you want the line to break.

Upvotes: 0

JaredPar
JaredPar

Reputation: 754763

Just insert a \n character to force a new line

const string PROGRAM_DESCRIPTION = "Program will calculate the amount "
"accumulated every month you save, \nuntil you reach your goal.";

Upvotes: 8

Related Questions