Double quotes without an escape sequence.

So, let's say I have something like this pop up. (Which, funnily, is on these same forums:)
http://www.cplusplus.com/forum/beginner/2355/

But, what if I can't do an escape sequence. What if I'm in a situation where I absolutely need the code to be in the way it was before this guy got it fixed, just working?

Is that even possible?

Thanks,
M260
No

C/C++, like a lot of programming languages, uses double quotes to delimit strings.

When the C++ compiler tokenizes this:

cout << "Say "hello" to the nice people!" << endl;

it gets the following tokens

1
2
3
4
5
6
7
8
cout
<<
"Say "
hello
"to the nice people!"
<<
endl
;


and will probably complain that you've made a failed attempt to use a variable called hello (e.g. undeclared identifier, missing operator, ...).

The \ tells the compiler that the next character in the string needs to be treated specially. In the case of \", it says this " is an actual " (part of the string) rather than the end of the string.

Other languages have their own way of solving this problem (e.g. VB does it by doubling the quote character to mean an actual quote)
Last edited on
But, what if I can't do an escape sequence. What if I'm in a situation where I absolutely need the code to be in the way it was before this guy got it fixed, just working?


Such a situation is impossible. No sense in worrying about "what ifs" that will never happen.
Thanks, everyone. &, well, I guess I'm f***'d.
LOL whats the problem master260?
For reals. I can't fathom a situation in where this would be any kind of problem.
MASTER260 wrote:
But, what if I can't do an escape sequence.

Why not? Did somebody remove the \ key from your keyboard?
Last edited on
Even then, there's char map.

Or copy/paste from this forum.

The only way changing it really isnt' an option is if the file is read only or something and you're forbidden to change it. But that's stupid.

"Here, fix this file that is broken and doesn't compile, but you can't make any changes to it."
How about hard coding it in to a variable then using that?
1
2
3
4
5
6
7
8
int main(){
	char *lQuote	=	new char[2];
	lQuote[0]	=	34;  // ascii for quote
	lQuote[1]	=	0;
	cout << "Say " << lQuote << "Hello" << lQuote << " to the nice people!";
	delete lQuote;
	return 0;
}
Last edited on
Another option is to use a compiler that supports the new C++ standard C++0x. That has syntax to accept raw string literals without any escapes: http://en.wikipedia.org/wiki/C%2B%2B0x#New_string_literals
 
const std::string literal = R"(Now "C++0x" has raw string literals)";
Last edited on
@James Burnby & Galik: Thanks, but James' method didn't work & Galik's is impossible for the same reason escape sequences are. (Not to be rude.)

Basically, the reason I need to do something like this is I'm essentially trying to enclose HTML in printf. The problem is, this is the code:
printf("<html><body><h1><a accesskey="Y" href="/home/master260/Downloads/Project Is This a Bad Game/Actual Work/HTML 2.html" target="_blank"><img src="/home/master260/Downloads/Project Is This a Bad Game/Actual Work/Start Menu.png" border="0" alt="Welcome to ITABG!"></a></h1></body></html>");

But seriously, thank you,
M260
Do I take it you that you want to literally include a bit of HTML in your cpp? As in

#include "example.html"

Because if you are cutting and pasting the HTML into you code, there's nothing to stop you editing the string so it's correctly escaped. And if you're loading the HTML from a file, it's a non-issue (the loaded characters will be whatever's in the file).

If so, write a script to escape the HTML file, writing the result to the temporary file, and then include that.

Andy
Last edited on
Unless I am missing something here there, I still don't see any reason you can just use escape sequences:

1
2
3
4
5
6
7
printf(
  "%s",
  "<html><body><h1><a accesskey=\"Y\" "
  "href=\"/home/master260/Downloads/Project Is This a Bad Game/Actual Work/HTML 2.html\" "
  "target=\" blank\"><img src=\"/home/master260/Downloads/Project Is This a Bad Game/Actual Work/Start Menu.png\" "
  "border=\"0\" alt=\"Welcome to ITABG!\"></a></h1></body></html>"
  );

BTW, be careful with the format string for printf() -- it is better to use "%s" and pass the text you want as argument to that.
The problem is, I'd imagine that'd interfere with the HTML. You see:

printf("<html><body><h1> & </h1></body></html>"); are C++ code.

<a accesskey="Y" href="/home/master260/Downloads/Project Is This a Bad Game/Actual Work/HTML 2.html" target="_blank"><img src="/home/master260/Downloads/Project Is This a Bad Game/Actual Work/Start Menu.png" border="0" alt="Welcome to ITABG!"></a is HTML.
Last edited on
What do you mean by interfere?

C++ won't interfere with your HTML. Just escape your quotes (and any other characters that need escaping... like '\') and you'll be fine.
Well, doesn't the HTML be in its exact form it would be even if I wasn't enclosing it with printf?
When you print it, the escaped characters are written as they 'should' be.
printf("stuff\"\"morestuff");
Prints:
stuff""morestuff

I don't understand what the problem would be.
Last edited on
Ah, I see. Well, thank you!
Hmm, well this is odd. I've been using Qt Creator & it seems to always output to something that just says, "Hello World!" I've concluded it does that whenever it doesn't have any errors, but it didn't recognize what you did, specifically, as if you hadn't input any code besides the default code in something made with Qt Creator.

Any help?

Once again, thank y'all for all this,
M260
Topic archived. No new replies allowed.