Failure to compile raw string literals for ascii art
Sep 14, 2020 at 4:51pm Sep 14, 2020 at 4:51pm UTC
So I'm trying to use ascii art in a game I'm writing for terminal, and the slashes cause errors because they are escape sequences. Supposedly using raw string literals should fix this problem. But my compiler (g++ on ubuntu 20.04) doesn't seem to recognize the R before my string, so I'm confused. The errors still show the escape sequences, but now also show "stray R in program".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = R" __ __.__ .___ __ __ .__ ." ;
string str2 = R"/ \ / \__|____________ _______ __| _/ / \ / \___________| | __| _/" ;
string str3 = R"\ \/\/ / \___ /\__ \\_ __ \/ __ | \ \/\/ / _ \_ __ \ | / __ |" ;
string str4 = R" \ /| |/ / / __ \| | \/ /_/ | \ ( <_> ) | \/ |__/ /_/ |" ;
string str5 = R" \__/\ / |__/_____ \(____ /__| \____ | \__/\ / \____/|__| |____/\____ |" ;
string str6 = R" \/ \/ \/ \/ \/ \/ \/" ;
return 0;
}
My terminal output is:
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
ame2.cpp:7:19: error: invalid character ' ' in raw string delimiter
7 | string str1 = R" __ __.__ .___ __ __ .__ ." ;
| ^
game2.cpp:7:16: error: stray ‘R’ in program
game2.cpp:8:20: error: invalid character ' ' in raw string delimiter
8 | string str2 = R"/ \ / \__|____________ _______ __| _/ / \ / \___________| | __| _/" ;
| ^
game2.cpp:8:16: error: stray ‘R’ in program
game2.cpp:9:19: error: invalid character '\' in raw string delimiter
9 | string str3 = R"\ \/\/ / \___ /\__ \\_ __ \/ __ | \ \/\/ / _ \_ __ \ | / __ |";
| ^
game2.cpp:9:16: error: stray ‘R’ in program
game2.cpp:10:19: error: invalid character ' ' in raw string delimiter
10 | string str4 = R" \ /| |/ / / __ \| | \/ /_/ | \ ( <_> ) | \/ |__/ /_/ |";
| ^
game2.cpp:10:16: error: stray ‘R’ in program
game2.cpp:11:19: error: invalid character ' ' in raw string delimiter
11 | string str5 = R" \__/\ / |__/_____ \(____ /__| \____ | \__/\ / \____/|__| |____/\____ |";
| ^
game2.cpp:11:16: error: stray ‘R’ in program
game2.cpp:12:19: error: invalid character ' ' in raw string delimiter
12 | string str6 = R" \/ \/ \/ \/ \/ \/ \/";
| ^
game2.cpp:12:16: error: stray ‘R’ in program
game2.cpp: In function ‘int main()’:
game2.cpp:8:17: warning: unknown escape sequence: ' \040'
8 | string str2 = R"/ \ / \__|____________ _______ __| _/ / \ / \___________| | __| _/";
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
game2.cpp:8:17: warning: unknown escape sequence: ' \_'
game2.cpp:8:17: warning: unknown escape sequence: ' \040'
game2.cpp:8:17: warning: unknown escape sequence: ' \_'
game2.cpp:9:17: warning: unknown escape sequence: ' \040'
9 | string str3 = R"\ \/\/ / \___ /\__ \\_ __ \/ __ | \ \/\/ / _ \_ __ \ | / __ |";
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
game2.cpp:9:17: warning: unknown escape sequence: ' \/'
game2.cpp:9:17: warning: unknown escape sequence: ' \/'
game2.cpp:9:17: warning: unknown escape sequence: ' \_'
game2.cpp:9:17: warning: unknown escape sequence: ' \_'
game2.cpp:9:17: warning: unknown escape sequence: ' \/'
game2.cpp:9:17: warning: unknown escape sequence: ' \040'
game2.cpp:9:17: warning: unknown escape sequence: ' \/'
game2.cpp:9:17: warning: unknown escape sequence: ' \/'
game2.cpp:9:17: warning: unknown escape sequence: ' \_'
game2.cpp:9:17: warning: unknown escape sequence: ' \040'
game2.cpp:10:17: warning: unknown escape sequence: ' \040'
10 | string str4 = R" \ /| |/ / / __ \| | \/ /_/ | \ ( <_> ) | \/ |__/ /_/ |";
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
game2.cpp:10:17: warning: unknown escape sequence: ' \|'
game2.cpp:10:17: warning: unknown escape sequence: ' \/'
game2.cpp:10:17: warning: unknown escape sequence: ' \040'
game2.cpp:10:17: warning: unknown escape sequence: ' \/'
game2.cpp:11:17: warning: unknown escape sequence: ' \_'
11 | string str5 = R" \__/\ / |__/_____ \(____ /__| \____ | \__/\ / \____/|__| |____/\____ |";
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
game2.cpp:11:17: warning: unknown escape sequence: ' \040'
game2.cpp:11:17: warning: unknown escape sequence: ' \_'
game2.cpp:11:17: warning: unknown escape sequence: ' \_'
game2.cpp:11:17: warning: unknown escape sequence: ' \040'
game2.cpp:11:17: warning: unknown escape sequence: ' \_'
game2.cpp:11:17: warning: unknown escape sequence: ' \_'
game2.cpp:12:17: warning: unknown escape sequence: ' \/'
12 | string str6 = R" \/ \/ \/ \/ \/ \/ \/";
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
game2.cpp:12:17: warning: unknown escape sequence: ' \/'
game2.cpp:12:17: warning: unknown escape sequence: ' \/'
game2.cpp:12:17: warning: unknown escape sequence: ' \/'
game2.cpp:12:17: warning: unknown escape sequence: ' \/'
game2.cpp:12:17: warning: unknown escape sequence: ' \/'
game2.cpp:12:17: warning: unknown escape sequence: ' \/'
Clearly I need to do some work to print out the ascii art.
Sep 14, 2020 at 4:56pm Sep 14, 2020 at 4:56pm UTC
Last edited on Sep 14, 2020 at 4:58pm Sep 14, 2020 at 4:58pm UTC
Sep 14, 2020 at 5:03pm Sep 14, 2020 at 5:03pm UTC
Oh, thank you, I kept reading that and I kept missing the parentheses, it compiled perfectly
Sep 14, 2020 at 5:04pm Sep 14, 2020 at 5:04pm UTC
To use raw strings, you need to enclose the string in (). So eg:
std::string str1 = R"(this is a raw string \ containing a back slash)" ;
Sep 14, 2020 at 5:09pm Sep 14, 2020 at 5:09pm UTC
I've got it done now and the final code looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = R"( __ __.__ .___ __ __ .__ .)" ;
string str2 = R"(/ \ / \__|____________ _______ __| _/ / \ / \___________| | __| _/)" ;
string str3 = R"(\ \/\/ / \___ /\__ \\_ __ \/ __ | \ \/\/ / _ \_ __ \ | / __ |)" ;
string str4 = R"( \ /| |/ / / __ \| | \/ /_/ | \ ( <_> ) | \/ |__/ /_/ |)" ;
string str5 = R"( \__/\ / |__/_____ \(____ /__| \____ | \__/\ / \____/|__| |____/\____ |))" ;
string str6 = R"( \/ \/ \/ \/ \/ \/ \/)" ;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << str4 << endl;
cout << str5 << endl;
cout << str6 << endl;
return 0;
}
Which outputs this to the terminal:
1 2 3 4 5 6 7
__ __.__ .___ __ __ .__ .
/ \ / \__|____________ _______ __| _/ / \ / \___________| | __| _/
\ \/\/ / \___ /\__ \\_ __ \/ __ | \ \/\/ / _ \_ __ \ | / __ |
\ /| |/ / / __ \| | \/ /_/ | \ ( <_> ) | \/ |__/ /_/ |
\__/\ / |__/_____ \(____ /__| \____ | \__/\ / \____/|__| |____/\____ |)
\/ \/ \/ \/ \/ \/ \/
Sep 14, 2020 at 7:23pm Sep 14, 2020 at 7:23pm UTC
sweet, your mud is nearly ready :P
Topic archived. No new replies allowed.