Exercise 0-2
Write a program that, when run, writes
This (") is a quote, and this (\) is a backslash.
Solution
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).
Result
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.