Accelerated C++ Solution to Exercise 0-2

Exercise 0-2

Write a program that, when run, writes

This (") is a quote, and this (\) is a backslash.


We know from exercise 0-0 that to output a string to the console window, the statement should look like this:

cout << "Some text";

From the book we also know that certain special string literals must be preceded by a backslash (\) should we wish to display these correctly inside the two double quotes.

The (double quote) and the \ (backslash) are one of these special literals and therefore require to be preceded by a \ (backslash) – the that the character display correctly. See the book for details.

The full program therefore should look like this:

#include <iostream>
    std::cout << "This (\") is a quote, and this (\) is a backslash" << std::endl;
    return 0;

Note that we have precede the (double quote) and the \ (backslash) with a \ (backslash).


Submit the program and we get the followings in the console window as required.

This (") is a quote, and this (\) is a backslash

Process returned 0 (0x0) execution time : 0.206 s
Press any key to continue.


Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2000

5 thoughts on “Accelerated C++ Solution to Exercise 0-2”

    1. Oh, my comment did not print correctly. Here is what I meant to say: shouldn’t the string literal “this (\) is a backslash” be written “this (\) is a backslash”?

      1. Aargh. My point was that it seems like the program should be using a double backslash in order for the single backslash to print.

  1. You are right, David. Correct answer:

    std::cout << “This (\”) is a quote, and this (\) is a backslash” << std::endl;

Leave a reply