How to print ascii art in c++?

may i know how can i cout


              _
              -=\`\
          |\ ____\_\__
        -=\c`""""""" "`)
           `~~~~~/ /~~`
             -==/ /
               '-'

               _  _
              ( `   )_
             (    )    `)
           (_   (_ .  _) _)
                                          _
                                         (  )
          _ .                         ( `  ) . )
        (  _ )_                      (_, _(  ,_)_)
      (_  _(_ ,)
 

this in c++??
because once i cout this, there will be error message:unknown escape sequence '\_'
Last edited on
Instead of couting everything, why don't you just read it from a file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <fstream>

#include <string>

std::string getFileContents (std::ifstream&);            //Gets file contents

int main(int argc, char *argv[])
{

    std::ifstream Reader ("File1.txt");             //Open file

    std::string Art = getFileContents (Reader);       //Get file
    
    std::cout << Art << std::endl;               //Print it to the screen

    Reader.close ();                           //Close file

    return 0;
}

std::string getFileContents (std::ifstream& File)
{
    std::string Lines = "";        //All lines
    
    if (File)                      //Check if everything is good
    {
	while (File.good ())
	{
	    std::string TempLine;                  //Temp line
	    std::getline (File , TempLine);        //Get temp line
	    TempLine += "\n";                      //Add newline character
	    
	    Lines += TempLine;                     //Add newline
	}
	return Lines;
    }
    else                           //Return error
    {
	return "ERROR File does not exist.";
    }
}
Because actually i'm doing something like a billboard for my program,the out put above is the first one,and will be follow by same output with a little difference,(the aeroplane move a bit to right)and then 3rd image....
same as the method we make GIF image animation..
just precede any nasty character with a backslash (\), that's the escape character, and it will stop the compiler complaints. Best way to do the animation would probably be to wait (see wait()) after every screen for the length of a frame, then use a clear screen command (in windows it's system("CLS") I think, but that's not particularly safe) and redraw.
Last edited on
Easy solution:
Replace all \ with \\.

The \ tells the compiler that "I am not a character, I am a command that will be combined with the next thing you see!". \n means next line, \t means tab, \0 means end-of-file and \\ means backslash. \" will let you use a quote in your output instead of just ending the string.

Cheers.
Last edited on
Hi Stewbond, after i change \ with \\, the program run successfully, but some other parts missing like
`""""""" "`)...etc

can you tell me anything else i can change??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	cout<<"#############################################################"<<endl;
	cout<<"#                    _                                      #"<<endl;  
	cout<<"#                  -=\\`\\                                    #"<<endl;  
	cout<<"#              |\\ ____\\_\\__                                 #"<<endl;  
	cout<<"#            -=\\c`""""""" "`)                               #"<<endl;  
	cout<<"#               `~~~~~/ /~~`\                                #"<<endl;  
	cout<<"#                 -==/ /                                    #"<<endl;  
	cout<<"#                   '-'                                     #"<<endl;  
	cout<<"#                  _  _                                     #"<<endl;  
	cout<<"#                 ( `   )_                                  #"<<endl;  
	cout<<"#                (    )    `)                               #"<<endl;  
	cout<<"#              (_   (_ .  _) _)                             #"<<endl;  
	cout<<"#                                             _             #"<<endl;  
	cout<<"#                                            (  )           #"<<endl;  
	cout<<"#             _ .                         ( `  ) . )        #"<<endl;  
	cout<<"#           (  _ )_                      (_, _(  ,_)_)      #"<<endl;  
	cout<<"#         (_  _(_ ,)                                        #"<<endl;  
	cout<<"#############################################################"<<endl;
A quote will end a string. so if you have
cout << "# "" #"<<endl;
Your compiler will read this as "# " and " #".

To display a quote correctly. Replace " with \" . Here we are using the backslash to tell the compiler to expect a special character. In this case we are getting the quote character instead of simply ending the string.

This output looks better to me now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	cout<<"#############################################################"<<endl;
	cout<<"#                    _                                      #"<<endl;  
	cout<<"#                  -=\\`\\                                    #"<<endl;  
	cout<<"#              |\\ ____\\_\\__                                 #"<<endl;  
	cout<<"#            -=\\c`\"\"\"\"\"\"\" \"`)                               #"<<endl;  
	cout<<"#               `~~~~~/ /~~`\                                #"<<endl;  
	cout<<"#                 -==/ /                                    #"<<endl;  
	cout<<"#                   '-'                                     #"<<endl;  
	cout<<"#                  _  _                                     #"<<endl;  
	cout<<"#                 ( `   )_                                  #"<<endl;  
	cout<<"#                (    )    `)                               #"<<endl;  
	cout<<"#              (_   (_ .  _) _)                             #"<<endl;  
	cout<<"#                                             _             #"<<endl;  
	cout<<"#                                            (  )           #"<<endl;  
	cout<<"#             _ .                         ( `  ) . )        #"<<endl;  
	cout<<"#           (  _ )_                      (_, _(  ,_)_)      #"<<endl;  
	cout<<"#         (_  _(_ ,)                                        #"<<endl;  
	cout<<"#############################################################"<<endl;
Last edited on
ok~thank you very much~~problem solved =)
Glad that the problem is solved; still posting this because it may be of interest.

One, if the compiler (for example GCC 4.5 or later) supports C++11 raw string literals, the entire problem with the backslash as an escape character can be avoided.
See: http://www2.research.att.com/~bs/C++0xFAQ.html#raw-strings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    std::cout << R"(
        #######################################
        #                    _                #
        #                  -=\\`\\            #
        #              |\\ ____\\_\\__        #
        #            -=\\c`""""""" "`)        #
        #               `~~~~~/ /~~`\         #
        #                 -==/ /              #
        #                                     #
        #######################################
        )" ;
}


Two, if the picture is made available as a sequence of strings, it can be easily manipulated by code. This would be a more flexible approach. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
std::vector<std::string> shift_right( std::vector<std::string>& picture,
                                      std::string::size_type n = 4 )
{
   // all error/bounds-checking is omitted for brevity
   const std::string& first = picture[0] ;
   const std::string::size_type size = first.size() ;
   const char border = first[0] ;
   const std::string spaces( n, ' ' ) ;

   for( std::string::size_type i = 1 ; i < ( picture.size() - 1 ) ; ++i )
       picture[i] = border + spaces + picture[i].substr( 1, size-n-2 ) + border ;

   return picture ;
}

void print( const std::vector<std::string>& picture )
{
    std::cout << '\n' ;
    for( const auto& line : picture ) std::cout << line << '\n' ;
    std::cout << '\n' ;
}

int main()
{
   std::vector<std::string> picture =
   {
       R"(#######################################################)",
       R"(#           _                                         #)",
       R"(#         -=\\`\\                                     #)",
       R"(#     |\\ ____\\_\\__                                 #)",
       R"(#   -=\\c`""""""" "`)                                 #)",
       R"(#      `~~~~~/ /~~`\                                  #)",
       R"(#        -==/ /                                       #)",
       R"(#                                                     #)",
       R"(#######################################################)"
   } ;

   print(picture) ;

   for( int i=0 ; i<5 ; ++i ) print( shift_right(picture,8) ) ;
}

@Enzo Ti

Instead of making a new image and cout it, why not just move the plane, and maybe a cloud, also? Like so..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Plane Billboard.cpp : Defines the entry point for the console application.

#include <stdafx.h> // Used with Visual C++. Remove if not needed
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

void gotoXY(int x, int y, string text);
void gotoXY(int x, int y);

int main(int)
{
	string Plane[]=
	{"                    _                                      "
	,"                  -=\\`\\                                    "
	,"              |\\ ____\\_\\__                                 "
	,"            -=\\c`\"\"\"\"\"\"\" \"`)                               "
	,"               `~~~~~/ /~~`                                "
	,"                 -==/ /                                    "
	,"                   '-'                                     "};

	string cloud[]=
	{"                  _  _                                     "
	,"                 ( `   )_                                  "
	,"                (    )    `)                               "
	,"              (_   (_ .  _) _)                             "};


	char holder = ' ';

	int len, x, y;

	len = Plane[0].length();
	string border(len,'\xCD');
	string shadow(len+2,'\xB2');
	cout << "\t  \xC9" << border << "\xBB" << endl;
	for (x=0;x<11;x++)
		cout<<"\t  \xBA                                                           \xBA\xB2"<<endl;  
	cout<<"\t  \xBA                                             _             \xBA\xB2"<<endl;  
	cout<<"\t  \xBA                                           (   )           \xBA\xB2"<<endl;  
	cout<<"\t  \xBA             _ .                         ( `  ) . )        \xBA\xB2"<<endl;  
	cout<<"\t  \xBA           (  _ )_                      (_, _(  ,_)_)      \xBA\xB2"<<endl;  
	cout<<"\t  \xBA         (_  _(_ ,)                                        \xBA\xB2"<<endl;  
	cout << "\t  \xC8" << border << "\xBC\xB2" << endl;
	cout << "\t   " << shadow << endl;
	do{
		for (y=0;y<7;y++)
		{
			gotoXY(11,1+y,Plane[y]);
		}
		
		for (x=0;x<4;x++)
		{
			gotoXY(11,8+x,cloud[x]);
		}

		for (y=0;y<7;y++)
		{
			holder = Plane[y][len];
			for (x=len;x>0;x--)
			{
				Plane[y][x]=Plane[y][x-1];
			}
			Plane[y][0]=holder;
		}

		for (y=0;y<4;y++)
		{
			holder = cloud[y][0];
			for (x=0;x<len;x++)
			{
				cloud[y][x]=cloud[y][x+1];
			}
			cloud[y][len]=holder;
		}
Sleep(60);
	}while(true);
return 0;
}

void gotoXY(int x, int y, string text) 
{ 
	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition);
	cout << text;
}

void gotoXY(int x, int y) 
{ 
	CursorPosition.X = x; 
	CursorPosition.Y = y;
}
Last edited on
Topic archived. No new replies allowed.