Problem With Visual Studio

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
You're going to have to list some errors, or the lines of code that cause the errors.
Amazingly, this is actually not enough information for us to know for sure what the problem is! I know, it's crazy. Maybe if you told us the error we could help you.
i copied and pasted this directly but it still shows error. can anybodie see errors in this :

//1.2 - Town Crier Program - Dirk Henkemans
#include <iostream>
#include <string>

int main(void)
{
using std::cout;
using std::string;
string yell = “A dragon is coming, take cover!!!\n”;
cout << yell.c_str() << “\n”
<< yell.c_str() << “\n”
<< yell.c_str() << “\n”
<< yell.c_str() << “\n”;
return 0;
}
The problem is that “ and ” should be replaced by ".
It compiled. Thanks a lot!! i'l just retype the examples instead of copy and paste :)
The above code has a bit of a problem.

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
#include <iostream>
#include <string>
int main(void)
{
    using std::cout;
    using std::string;
    string yell = "A dragon is coming, take cover!!!\n";
    cout << yell << "\n"
         << yell << "\n"
         << yell << "\n"
         << yell << "\n";
    return 0;
}


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()!

Andy

PS This c_str() mistake is repeated thoughout the book!

PPS The function required to write a string to cout is, of course:

ostream& operator<< (ostream& os, const string& str);
http://www.cplusplus.com/reference/string/operator%3C%3C/

(defined in <string>)
Last edited on
Topic archived. No new replies allowed.