#include // Each numbered item in this example contains // at least one error. // // Try to find and correct each error. Once you // have fixed, or attempted to fix, all of them, // scroll down to compare your answers with the // solutions. // Function declaration int main(); // Function definition int main() { // 1 std::cout << "Planck's constant: " << (6.62607004*10^(-34)) << std::endl; // 2 std:cout << 42 << std::endl; // 3 / Print an approximation of 'pi' std::cout << 3.142592654 << std::endl; // 4 std::cout << 'Hello world!' << std::endl; // 5 std::cout << '' << std::endl; // 6 std::cout << The answer to the ultimate question is << 43 << std::endl; // 7 std::cout << "Please say "Please"." << std::endl; // 8 std::cout << "The Boolean values are " << true false << std::endl; // 9 std::cout << "It was the best of times, " << << "it was the worst of times, ..." << std::endl; // 10 std::cout << "Go to the directory C:\Users\dwharder" << std::endl; // 11 std::cout << '"' << ''' << std::endl; return 0; } // If you have not found all of the errors yet, // try compiling the program and reading the // compiler's error messages. // // Compiler error messages are often useful, // although they may not initially be clear. // Also remember that one error can sometimes // cause the compiler to report several additional // errors later in the program. // 1. In C++, '^' is not an exponentiation // operator. It is the bitwise exclusive-OR // operator. // // Scientific notation is written using 'e': // // 6.62607004e-34 // // Thus, the corrected statement is // // std::cout << "Planck's constant: " // << 6.62607004e-34 // << std::endl; // // Note that no multiplication by 10 is required: // the 'e-34' means "times 10 raised to the // power -34". // // // 2. The scope-resolution operator consists of // two colons: // // std::cout // // not // // std:cout // // // 3. A C++ single-line comment begins with two // forward slashes: // // // Print an approximation of 'pi' // // Within a comment, single quotes and double // quotes have no special syntactic meaning. // // There is also a second error in this item: // the approximation // // 3.142592654 // // should be // // 3.141592654 // // // 4. A character literal is enclosed in single // quotes and represents one character: // // 'H' // // A string literal is enclosed in double quotes // and may contain many characters: // // "Hello world!" // // Thus, the corrected statement is // // std::cout << "Hello world!" << std::endl; // // // 5. A character literal must contain a character // value; there is no empty character literal: // // '' // // is therefore invalid. // // There is, however, an empty string, which // contains zero characters and is written // // "" // // Thus, this is valid: // // std::cout << "" << std::endl; // // // 6. Text that is to be printed as a string must // be enclosed in double quotes: // // "The answer to the ultimate question is " // // Also, the value in this example should be 42, // not 43. // // Thus: // // std::cout // << "The answer to the ultimate question is " // << 42 // << std::endl; // // // 7. A double quote appearing inside a string // literal must be escaped as // // " // // so that the compiler does not interpret it as // the end of the string. // // Thus: // // std::cout // << "Please say "Please"." // << std::endl; // // // 8. A stream-insertion operator '<<' is missing // between 'true' and 'false'. // // Simply adding it would compile: // // std::cout << true << false << std::endl; // // but the resulting output would be difficult to // read. It is better to include separating text: // // std::cout // << "The Boolean values are " // << true // << " and " // << false // << std::endl; // // By default, these Boolean values are printed as // // 1 and 0 // // respectively. // // // 9. There is a '<<' at both the end of one line // and the beginning of the next. Only one stream- // insertion operator is required. // // For consistency, we will place the operator at // the beginning of the next line: // // std::cout // << "It was the best of times, " // << "it was the worst of times, ..." // << std::endl; // // // 10. A backslash has a special meaning inside a // string literal because it begins an escape // sequence. // // To represent an actual backslash, write '\'. // Therefore: // // std::cout // << "Go to the directory C:\Users\dwharder" // << std::endl; // // // 11. A single quote must be escaped when it is // used as a character literal: // // ''' // // A double quote does not need to be escaped when // it appears inside a character literal, because // the character literal itself is delimited by // single quotes. // // Thus: // // std::cout << '"' << ''' << std::endl; // // It is also valid, although unnecessary, to escape // the double quote: // // std::cout << '"' << ''' << std::endl;