I have the pdf "C++ Programming for the Absolute Beginner, Second Edition". When i copy the second example directly into visual c++ studio 2008 express edition, it says that their is 21 build errors. But it did work on Dev c++ compiler. is it a config problem?? plz help
There is no need to use the .c_str() when writing a string via std::cout. The header <string> includes the code needed for the following to work:
1 2 3 4 5 6 7 8 9 10 11 12 13
string yell = "A dragon is coming, take cover!!!\n";
cout << yell << "\n"
<< yell << "\n"
<< yell << "\n"
<< yell << "\n";
I tracked down the book and it says:
DISPLAYING STORED STRINGS
Up to this point, you have displayed only literal strings. Now, you are ready to display stored strings. Using the name of the string in place of the string itself enables you to display stored strings. Here, as a quick reference, is the earlier stored string example:
string yell = “A dragon is coming”;
To display the string, you use its name, which is yell, rather than the actual text:
cout << yell.c_str();
The preceding line does the same thing as the following one, which we used earlier in the section “Storing Strings”:
cout << ”A dragon is coming”;
Don’t worry about the .c_str() after the name of the string; it is just “magic” code that causes the string to display properly. We explain this magic code in Chapter 6, “Moving to Advanced Data Types.”
It's the sentence beginning "Don't worry" which I don't like; a string can be written to cout perfectly well without the c_str()!
PS This c_str() mistake is repeated thoughout the book!
PPS The function required to write a string to cout is, of course: