In C ++, how to print ASCII art to the console?

Say you want to print one of these magnificent ASCII images. How can you do this without cout each line individually?

+6
source share
4 answers

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

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

Or, more precisely, it is possible:

 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 slightly different from "\ n" after each line, because you also clear the output stream.

+17
source

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 such as backslashes \, without having to use two of them, it displays everything and does not recognize control codes such as \ n, etc. Very comfortably.

Edit: this is called a "raw string literal" and was added in C ++ 11. You can find more information about the commands here: https://en.cppreference.com/w/cpp/language/string_literal

+8
source

Others have already suggested using endl . Although this is (not necessarily) bad, using endl clears the stream buffer along with writing a new line. Unlike one of the answers you received, using endl does not help (in general) when translating a new line to any sequence of characters that the platform usually uses to signal the end of a line. Using newline guaranteed to be exactly equivalent to stream << "\n" << flush;" . Newlines are translated to" \ r "or" \ n "or" \ r \ n "or whatever the platform prefers, runs on another level, and newline has nothing to do with it.

However, flush , what it does, can (and often will) slow down your I / O, sometimes by a pretty significant margin. While you are writing a few lines (for example, a couple of hundred characters), this probably does not matter at all. However, if you write a large file, using endl instead of "\n" can lead to 10x slowdowns (in fact, I would say that most of the idea that iostreams are slow stems directly with endl ).

Not to say, there was never a reason to use endl. flush ensures that everything written in the stream is immediately flushed from the standard library buffer and sent to the OS. If you want to provide an immediate display, endl may be useful. Similarly, if you are logging, and it is important that your log always reflects the latest known state of the program, endl can be (extremely) useful to ensure that what you write is actually logged and not lost in the buffer when / if the application crashes.

Thus, endl makes sense from time to time, but probably in 95% of the cases when it was used, it is really inappropriate (for example, it is unlikely that it will do anything useful in any of the answers to this question).

+2
source

This is pretty simple, fortunately. Just use endl when you want to start another line.

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

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

0
source

Source: https://habr.com/ru/post/1300004/


All Articles