Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why when i add \ to cout it dosent display it on output

When I try to do printf("----/");, the \ is removed from the output

#include <iostream>

int main() {
    std::cout << ("Welcome to the game\n\n");

    printf("--------\n");
    printf("----O---\n");
    printf("---/|\--\n");
    printf("---/-\--\n");
}

output:

--------
----0---
---/|--
---/---

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The answer has been given already: \ is special, because it’s the escape character, and you need to escape it with itself if you want a literal \ in the outoupt.

This, however, can make the drawing confused.

To improve the things, you can use raw strings literals

    printf(R"(--------\n)");
    printf(R"(----O---\n)");
    printf(R"(---/|\--\n)");
    printf(R"(---/-\--\n)");

Clearly, you can still have issues, if a string contains exactly the characters )", because it will be detected as the closing token of the string:

//               +---- code invalid from here
//               |
//               v
printf(R"(-----)"---\n)");

but you can still solve it, for instance by adding another set of wrapping "s:

printf(R""(-----)"---\n)"");

or anything else in between the " and (, and between ) and " (not symmetrized):

printf(R"xyz(-----)"---\n)xyz");
//                        ^
//                        |
//                        +--- not zyx
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading