I was trying to make a simple calculator and I would like to display quotes in the instruction when you first run the program.
-
I assume you've just started with C++? I feel this question is more suited for Google than StackOverflow. – swinefish Jan 21 '16 at 07:14
2 Answers
Another solution is to use raw strings:
#include <string>
#include <iostream>
int main()
{
std::cout << R"_(A raw string with " inside (and \ as well))_" << std::endl;
return 0;
}
Output:
A raw string with " inside (and \ as well)
Quotes from the Standard:
According to the Standard 2.14.5 [lex.string]:
string-literal:
encoding-prefixopt "s-char-sequenceopt"
encoding-prefixopt R raw-string
encoding-prefix:
u8
u
U
L
s-char-sequence:
s-char
s-char-sequence s-char
s-char:
any member of the source character set except
the double-quote ", backslash \, or new-line character
escape-sequence
universal-character-name
raw-string:
" d-char-sequenceopt ( r-char-sequenceopt) d-char-sequenceopt "
r-char-sequence:
r-char
r-char-sequence r-char
r-char:
any member of the source character set, except
a right parenthesis ) followed by the initial d-char-sequence
(which may be empty) followed by a double quote ".
d-char-sequence:
d-char
d-char-sequence d-char
d-char:
any member of the basic source character set except:
space, the left parenthesis (, the right parenthesis ), the backslash \,
and the control characters representing horizontal tab,
vertical tab, form feed, and newline.
A string literal is a sequence of characters (as defined in 2.14.3) surrounded by double quotes, optionally prefixed by
R,u8,u8R,u,uR,U,UR,L, orLR, as in"...",R"(...)",u8"...",u8R"**(...)**",u"...",uR"*˜(...)*˜",U"...",UR"zzz(...)zzz",L"...", orLR"(...)", respectively.A string literal that has an
Rin the prefix is a raw string literal. Thed-char-sequenceserves as a delimiter. The terminatingd-char-sequenceof a raw-string is the same sequence of characters as the initiald-char-sequence. Ad-char-sequenceshall consist of at most 16 characters.- [Note: The characters
(and)are permitted in a raw-string. Thus,R"delimiter((a|b))delimiter"is equivalent to"(a|b)". —end note ][Note: A source-file new-line in a raw string literal results in a new-line in the resulting execution
string-literal. Assuming no whitespace at the beginning of lines in the following example, the assert will succeed:const char *p = R"(a\ b c)"; assert(std::strcmp(p, "a\\\nb\nc") == 0);— end note ]
[Example: The raw string
R"a( )\ a" )a"is equivalent to
"\n)\\\na\"\n". The raw stringR"(??)"is equivalent to
"\?\?". The raw stringR"#( )??=" )#"is equivalent to
"\n)\?\?=\"\n". —end example ]
- 32,469
- 11
- 74
- 99
-
-
I was thinking about that, maybe a bit overkilling for a double quote, but I love it!! Upvoted. – skypjack Jan 21 '16 at 07:13
-