# Exercise 1_2 multiple console outputs. I would like to reduce the number of console output statements

Please could we examine the below code for anyway to put all console output in one statements?

 #include <iostream>
using namespace std;

int main( )
{  //print("") in Python
cout<<"Hello " "everybody!"<<endl;
cout<<"My name is AK."<<endl;
cout<<"Goodbye."<<endl;
cout<<""<<endl;

//Poem
cout<<"Twinkle, twinkle, little bat!"<<endl;
cout<<"How I wonder what you're at?"<<endl;
cout<<"Up above the world you fly,"<<endl;
cout<<"Like a tea-tray in the sky."<<endl;
return 0;
}

• You can just use '\n' literals inside the text you want to output, or even better raw string literals. – πάντα ῥεῖ Jan 30 at 21:19
• i did this #include <iostream> using namespace std; int main( ) { //print("Hello World") //Displays Hello World on the screen // Short form with string literals string Intro = "Hello everybody!"<<endl "My name is AK".\n "Goodbye."\n \n "Twinkle, twinkle, little bat!"\n "How I wonder what you're at"?\n "Up above the world you fly,"\n "Like a tea-tray in the sky."; cout<< Intro<<end1; return 0; } – Megadon.cyber Jan 30 at 21:45

## Scope

This is really just limited to style and code formatting.

## Two (almost) substantive points

• You should get in the habbit of not:
using namespace std;


It can get you into name-clash issues later, as you progress.

• You should be aware that std::endl flushes the stream buffer. This is often unnecessary, unwanted and can be slow if used in a tight loop. You should use '\n' for most situations. If you are worried about platform specific line-ending, don't be. '\n' adapts, just like std::endl.

## Code formatting

All I did was to integrate your endl into the strings and hit "auto-clang-format" and I got this:

#include <iostream>

int main() {
std::cout << "Hello everybody!\n"
<< "My name is AK.\n"
<< "Goodbye.\n"
<< "\n"

// Poem
<< "Twinkle, twinkle, little bat!\n"
<< "How I wonder what you're at?\n"
<< "Up above the world you fly,\n"
<< "Like a tea-tray in the sky.\n";
return 0;
}



Which is "good enough for me". Clang format is very tunable, and I have it configured to something which works for me, and my team, in 99% of cases. So we don't spend time fighting the formatting of the code.

The above style with "one streaming operator" << at the beginning of each line is what we use most of the time. It makes sense when you have literals interspersed with variables and/or function calls.

For this very specific (and rather atypical?) case, you could, as someone else pointed out, also just stream it all as one continuous literal. C++ allows you stop/start string literals like this:

#include <iostream>

int main() {
std::cout << "Hello everybody!\n"
"My name is AK.\n"
"Goodbye.\n"
"\n"

// Poem
"Twinkle, twinkle, little bat!\n"
"How I wonder what you're at?\n"
"Up above the world you fly,\n"
"Like a tea-tray in the sky.\n";
return 0;
}



Hope that helps.

• Why not using raw string literals for the whole text? Just to preserve the comments? – πάντα ῥεῖ Jan 30 at 22:19
• @πάνταῥεῖ Yes, as it is all one string, you could in this case. Have added that. – Oliver Schönrock Jan 30 at 22:29
• Are you sure what raw string literal means? – πάντα ῥεῖ Jan 30 at 22:31
• @πάνταῥεῖ It's not something I get exited about, but yes I do. And I don't see the point here. It's just messy. but that's entirely subjective. – Oliver Schönrock Jan 30 at 22:34
• I am sorry for the messy cod. I hope i can soon learn how to write cleaner code. Thank you for the string literals direction. I found out why you used R"x as it outputs raw data. You guys are the best. – Megadon.cyber Jan 31 at 5:06

You can do that in single statements using raw string literals:

#include <iostream>

int main( )
{  //print("") in Python
std::cout <<
R"x(Hello everybody!
My name is AK.
Goodbye.
)x";

//Poem
std::cout<<
R"x(Twinkle, twinkle, little bat!
How I wonder what you're at?
Up above the world you fly,
Like a tea-tray in the sky.
)x";
return 0;
}

• counter nitpick: don't use namespace std. That's 2 statements, not one. And it looks messy. IMHO. ;-) Indentation is totally broken if you used that in a class or nested block. – Oliver Schönrock Jan 30 at 22:35
• Thank you very much good people. I have just started learning so my apologies that it looks like a dog's breakfast. – Megadon.cyber Jan 31 at 4:46
• Hi, i am trying to mark/upvote both answers as very useful but it won't let me. What have i done wrong because both answers are correct? – Megadon.cyber Jan 31 at 5:10
• @Megadon.cyber You can upvote both, but not accept both I think. Don't sweat it. – Oliver Schönrock Jan 31 at 5:16
• I generally advise against multi-lines raw string literals because they mess up with indentation. The parser doesn't care, but the human reader does. – Matthieu M. Jan 31 at 9:17