In C++, how to print ASCII art to the console?
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.
try something like:
cout << R"(place multiple lines
of text here
and it will display exactly
as you have it between the two brackets,
line feeds and all.)";
...the above code will also allow you to use characters like the backslash \ without needing two of them, it displays everything and doesn't recognize control codes, like \n etc. Very handy.
This is called a "string literal" and was added in C++11. You can find more information on the commands here, specifically refer to the prefix "R" which is for raw_characters: https://en.cppreference.com/w/cpp/language/string_literal
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).