How can I put sentences in more than a line ??

+1 vote
asked Aug 31 by Alreem (320 points)
How can I put sentences in more than a line ??

for example I use “enter” in my keyboard when I’m typing how about when we use the c++

3 Answers

0 votes
answered Sep 1 by Roshan (180 points)
If you want to take a complete sentence then you have to write in a same line, else there is no other way.

Because its not an editor where you can hit enter and it will go to the next line. Enter tells that line is terminated.
+1 vote
answered Sep 1 by Peter Minarik (7,830 points)

Breaking instructions into multiple lines

If you're asking about writing code, then code can be broken in any arbitrary number of lines where a token ends.

Look at the following loop:

int sum = 0;
for (int i = 0; i < 10; i++)
    sum += i;

This could be written as a single line:

int sum = 0; for (int i = 0; i < 10; i++) sum += i;

Or you can break it into multiple lines where a token ends (so do not break in the middle of a word). E.g.:

int sum = 0;
    int i = 0;
    i < 10;


These are all equivalent and generate the same machine code.

Breaking strings into multiple lines

If you want to break strings into multiple lines, all you need to do is close the string before the end of the line with a quote and continue in the next line within a new pair of quotes:

std::string cppString = "This string"
"really is only"
"one line";
char * cString = "This is"
                 "also just"
                 "one line, when printed".

As you can see, the compiler ignores any while space when you break strings up into multiple lines in the code (yet they remain a single line for the compiler).

The backslash

With the use of backslash (\), you tell the compiler that you want to continue the line where you are. You can place it anywhere (even within words). The compiler will glue the next line to the end of this one. For this, white spaces are not to be ignored.

So this one works just fine:

std::string myString = "This is\
also a single line";
int sum = 3;
m +\
= 15; // sum += 15. You can place the backslash within identifiers (sum) or withing operators (+=) too.

However, due to the backslash (\) taking white space into account, the following do not work:

int sum = 3;
sum +\
    = 15; // The compiler does not understand what sum +     = 15 is. We wanted sum += 15, but what we wrote above was + operation followed by = operation.
0 votes
answered Sep 1 by B17CS164 (150 points)

cout<<"This is a sentence.\nAnd another Sentence.";

the above line give output as:

This is a sentence.

And another Sentence.

here '\n' is an escape sequence, which is called 'new line character' or 'line feed', used to move the cursor to next new line.

In c++, alternatively use 'std::endl' ,stands for end line. 


cout << "This is a sentence." << endl << "And another Sentence.";

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.