0

I am trying to print stuff like this like an exercise:

  *
 ***
*****
 ***
  *

and I have been using c++ to print every line in a separate statement. It can pass the tests all right.

However, I have learnt in python that we can do something like this:

s = """
  *
 ***
*****
 ***
  *
"""
print(s)

and it does the job neatly. So I have been wondering: Is there an elegant equivalent in c++? The searching I have used all need using \n or std::endl as line breaks, which I do not want to do.

Benny Frog
  • 11
  • 5
  • 1
    the duplicate is an old question, hence the most up to date [answer](https://stackoverflow.com/a/5460235/4117728) is not the accepted or highest voted one – 463035818_is_not_an_ai Jun 29 '23 at 08:33
  • fwiw the point of such exercises is to practice loops. Rather than using a raw string literal, I suggest to use the `std::string` constructor that takes a character and number of times to repeat, which can help a lot in such exercises without avoiding all the loops – 463035818_is_not_an_ai Jun 29 '23 at 08:35
  • You can use [raw string literals](https://en.cppreference.com/w/cpp/language/string_literal). – Jesper Juhl Jun 29 '23 at 12:00

1 Answers1

0

Just use multiline string literals, like this :

#include <iostream>

const char *s= "  *  \n"
               " *** \n"
               "*****\n"
               " *** \n"
               "  *  \n";

int main()
{
    std::cout << s;
    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19