Goldname Goldname - 1 month ago 16
C++ Question

How can I add newlines to stringstream or a string using returns rather than '\n'?

I have a file. Rather than using ifstream, I want to hard code it into my .cpp file. However, it is formatted using returns rather than "\n" characters. Is there a solution for this? I don't want to manually convert everything to "\n".

stringstream newString;
newString = "Nine of Spades
Ten of Spades
Jack of Spades
Queen of Spades
King of Spades
Ace of Spades
Nine of Hearts
...
Ace of Diamonds";

Answer

You can use a raw string literal:

#include <sstream>

std::stringstream newString{
R"(Nine of Spades
Ten of Spades
Jack of Spades
Queen of Spades
King of Spades
Ace of Spades
Nine of Hearts
...
Ace of Diamonds)" };

int main() {

    return 0;
}

live demo