tags:

views:

127

answers:

4

Let's say you wanted to print out one of those great ascii art images. How can you do that without "cout"ing each line individually?

+9  A: 

Adjacent string literals are concatenated, so you can do this:

cout << " _______________________ _______ _          _______         _______ _______  _______ _       _______         \n"
        "(  ____ \__   __/  ___  )  ____ \ \    /\  (  ___  )\     /|  ____ \  ____ )(  ____ \ \     (  ___  )\     /|\n"
        "| (    \/  ) (  | (   ) | (    \/  \  / /  | (   ) | )   ( | (    \/ (    )|| (    \/ (     | (   ) | )   ( |\n"
        "| (_____   | |  | (___) | |     |  (_/ /   | |   | | |   | | (__   | (____)|| (__   | |     | |   | | | _ | |\n"
        "(_____  )  | |  |  ___  | |     |   _ (    | |   | | (   ) )  __)  |     __)|  __)  | |     | |   | | |( )| |\n"
        "      ) |  | |  | (   ) | |     |  ( \ \   | |   | |\ \_/ /| (     | (\ (   | (     | |     | |   | | || || |\n"
        "/\____) |  | |  | )   ( | (____/\  /  \ \  | (___) | \   / | (____/\ ) \ \__| )     | (____/\ (___) | () () |\n"
        "\_______)  )_(  |/     \|_______/_/    \/  (_______)  \_/  (_______//   \__/|/      (_______/_______)_______)\n";

Or, more accurately, perhaps:

cout << "                              .::/-                                                                                                                                                             \n"
        "                              .+++/                                                                                                                                                             \n"
        "                   `.::`       /+++.                                                                                                                                                            \n"
        "                  -////.      :+++-                                                                                                                                                             \n"
        "                    .////-`    .+++/`                                                                                                                                                           \n"
        "                    `:///:`   `/++/.                                                                                                                                                            \n"
        "            ..`        -////.   -+++:                                                                                                                                                           \n"
        "           :+++:-`      .////:` ./++/`                                                                                                                                                          \n"
        "          `-/+++++/-.    `:////.`:++/.                                                                                                                                                          \n"
        "             `.:/++++/:.`  -////..:--`                                                                                                                                                          \n"
        "                 .-/+++++/-..::.`                                                                                                                                                               \n"
        "       `:::-..``    `.:/++++-                                                                                                                                                                   \n"
        "       -++++++///:--.```.-/-                                                                                                                                                                    \n"
        "        `.--:///++++++//::.                                                                                                                                                                     \n"
        "`--.          ``..-::///+/``---                                                 -+-                                                             ./oso- /++:                                     \n"
        "-oo+  -::::----....````... `ooo                  :s-                            /mo                                                            -dmmhy:`hmmo                                     \n"
        "-oo+  /+++++++++++++++++/. `ooo                  om:                            /mo             ````     ```      ```    ``.``      ```  `.``  ommd`` `hmmo       ``.``    ```     ```     ```  \n"
        "-oo+  ...----::::////+++/` `ooo      `/ssyss+:`.ohmyoo` .+ssyss+-     -+syys+-  /mo    -o+.  .ohdmmdho- -hdd/   `sdds` :shmmmdy/`  .hddshdmmhoydmmmhy:`hmmo    .+hdmmmds- .ddd/   .ddh-   +ddh. \n"
        "-oo+  ```````````````````  `ooo     .yh-.``-/-  .sm/.` `/o-```-sd+  .sd+-..-++` /mo  .odo.  :dmmy+/smmm: +mmh-  /mmd- +mmh+:/smmy- .dmmdo/+s:`/ymmm++.`hmmo   .dmmh++smmd+`ommd` `ymmmy  .hmm+  \n"
        "-oo+  +oooooooooooooooooo- `ooo     -dy.         om:           -dy` +m/         /mo`+dy-   `smmy`  `smmy``smms`.hmm/ -dmd+---:hmmo`.dmm+       ommd   `hmmo   ommh.   ommh..ymm+ +mmdmm/ ommy.  \n"
        "-oo+  /++++++++++++++++++. `ooo      -oyhyyys/`  om:   `:osyyyyymy``sm-         /myhyhd:   `smms    +mmh` `dmm/smms  :dmmddddddddo`.dmm/       ommd   `hmmo   smmy`   /mmd. :dmd+dmy-ymd+hmd:   \n"
        "-oo+                       `ooo          ``.+do  om:   /do.    -dy``om:         /md/``od+` `ommh.  `ymmy`  :dmmmmy.  .hmd/`````.`  .dmm/       ommd    hmmo   +mmh-   smmy` `smmmmm- :dmmmmo    \n"
        "-oo+:::::::::::::::::::::::/ooo     -+:.```.od+  +mo.` /do.```.omy` .sd/.``.//` /mo    +dy. -ymmdysdmmh-    +mmmh-    :dmmyoosdd+` .dmm/       ommd    ommmso.`ymmdyshmmh:   .hmmm+   +mmmd`    \n"
        "-oooooooooooooooooooooooooooooo     ./syyyyyo:`  `:sys.`:syyyys+yo`  `:syyyyo:` :h/     :ys` `:shddhs/`     `ohy/      ./shddhy+-  .shh:       /hhy    `:syhs. `:oyhdhs/.     /hho`   `shh/     \n"

More sensibly, use endl. This is subtly different from just "\n" after each line, because you'll also flush the output stream.

John Feminella
You want to add newline's to that string or your code will depend on the width of the display.
R Samuel Klatchko
Fixed that! Thanks for the catch.
John Feminella
@John: nice ascii graphics - you use figlet for that? +1 from me... :)
tommieb75
It's the "Epic" font, from patorjk.com's generator.
John Feminella
A: 

Please check: Is there a way to delimit strings in C++ like PHP’s Heredoc Syntax?

Alan Haggai Alavi
C++0x has raw string literals which do the same.
Tronic
+2  A: 

It's pretty simple thankfully. Just use endl whenever you want to start another line.

cout << stuff << endl
     << morestuff << endl
     << evenmorestuff << endl;

I would like to state that I much prefer using endl because it should work even if you are on a platform that requires "\r\n" instead of just "\n".

While an obvious answer, printing many lines this way may become a burden for the CPU if done frequently. Each use of the overloaded operator << is hiding a function call behind it, so printing multiline text requires MANY function calls.
mingos
Yes it can become a burden done that way but if all he is doing is printing a bit of ascii art, it shouldn't be a problem. The other way would be to do the work of packing it all into one big string and shoving it all out at once. I have had to do that on occasion but most of the time, this method isn't a problem.
Your remark about `std::endl` is silly. Even if you are on a platform that requires `\r\n` instead of `\n` as its native line terminator, `\n` is still the newline terminator in C and C++, and the I/O libraries convert it for you. (This is the difference between text-mode and binary-mode streams.) `std::endl` is somewhat less efficient than raw `\n` since it unnecessarily flushes the stream.
jamesdlin
You're probably right but I did have problems with it many many years ago. Next to nothing being used these days has problems with just using '\n'.
+1  A: 

Others have already suggested using endl. While this isn't (necessarily) a bad thing, using endl flushes the stream's buffer along with writing a new-line. Contrary to the implication in one of the answers you've gotten, using endl does not help (at all) with translating the new-line to whatever character sequence the platform normally uses to signal the end of a line. Using newline is guaranteed to be precisely equivalent to stream << "\n" << flush;". Translating new-lines to "\r", or "\n" or "\r\n", or whatever the platform prefers, is done at a different level and newline has nothing to do with it.

The flush that it does, however, can (and often will) slow down your I/O, sometimes by quite a considerable margin. As long as you're only writing a few lines (e.g. a couple hundred characters) it's probably completely irrelevant. If you're writing a large file, however, using endl instead of "\n" can easily result in a 10x slowdown (in fact, I'd go so far as to say that much of the idea that iostreams are slow stems directly from using endl).

That's not to say there's never any reason to use endl. The flush assures that whatever has been written to the stream is immediately flushed out of the standard library's buffer, and sent to the OS. If you want to assure immediate display, endl can be useful. Likewise, if you're doing logging, and it's critical that your log always reflect the most recent known state of a program, endl can be (extremely) useful to assure that what you've written really gets logged, not lost in a buffer when/if the application crashes.

So, endl makes sense at times, but probably 95% of the time that it's used, it's really inappropriate (e.g., it's unlikely to accomplish anything useful in any of the answers to this question).

Jerry Coffin