4
\$\begingroup\$

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;
}
\$\endgroup\$
  • \$\begingroup\$ You can just use '\n' literals inside the text you want to output, or even better raw string literals. \$\endgroup\$ – πάντα ῥεῖ Jan 30 at 21:19
  • \$\begingroup\$ 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; } \$\endgroup\$ – Megadon.cyber Jan 30 at 21:45
7
\$\begingroup\$

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

  • Install clang-format or similar to help you with 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.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Why not using raw string literals for the whole text? Just to preserve the comments? \$\endgroup\$ – πάντα ῥεῖ Jan 30 at 22:19
  • 1
    \$\begingroup\$ @πάνταῥεῖ Yes, as it is all one string, you could in this case. Have added that. \$\endgroup\$ – Oliver Schönrock Jan 30 at 22:29
  • \$\begingroup\$ Are you sure what raw string literal means? \$\endgroup\$ – πάντα ῥεῖ Jan 30 at 22:31
  • \$\begingroup\$ @πάνταῥεῖ 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. \$\endgroup\$ – Oliver Schönrock Jan 30 at 22:34
  • \$\begingroup\$ 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. \$\endgroup\$ – Megadon.cyber Jan 31 at 5:06
2
\$\begingroup\$

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;
}
| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Oliver Schönrock Jan 30 at 22:35
  • \$\begingroup\$ Thank you very much good people. I have just started learning so my apologies that it looks like a dog's breakfast. \$\endgroup\$ – Megadon.cyber Jan 31 at 4:46
  • \$\begingroup\$ 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? \$\endgroup\$ – Megadon.cyber Jan 31 at 5:10
  • \$\begingroup\$ @Megadon.cyber You can upvote both, but not accept both I think. Don't sweat it. \$\endgroup\$ – Oliver Schönrock Jan 31 at 5:16
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – Matthieu M. Jan 31 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.