The book is wrong and so are any other sources you have found that suggest that this is correct. The book is not only wrong but very out of date. I believe that it also comes with an out of date version of Dev-C++ (but it seems not so out-of-date that the code in the book works - if it ever did!).
Most of what the book teaches can be 'fixed' by learning about deprecated header forms and namespaces (but not if you are using the version that came with the book perhaps - you'll need v4.9.9.2). If you want an example of a working C++ "hello, world" try the template project provided by Dev-C++ (File->New->Project->Introduction).
So you should do the following:
1) Read the "PLEASE READ BEFORE POSTING A QUESTION" thread on this forum.
2) The thread in (1) tells you how to uninstall your old version and install the new version in a way that won't cause conflicts.(it is not as simple as you might like!). You should do that - not least because it is hard for us to support you with a long abandoned version that none of us have installed.
3) Read the rest of that thread it tells you many things you will need to know, including:
--a) How to post questions on the forum (version number, OS, compile log, as well as code)
--b) How to stop the program terminating and closing the window before you see the output
Anyway the fundamental problem with your code is that <string.h> is the C string library header not the C++ string class header. That is simply <string> with no .h. In C++ all the .h forms are deprecated for standard library headers. C library headers acquire a 'c' prefix, allowing <string> to be distinct from <cstring> (equivalent to the old <string.h>)
It is anyone's guess why the code from the book includes <stdlib.h> or <stdio.h> since the code does not use anything from those headers. If this is indicative of the quality of the whole book, you need to proceed cautiously. The arguments to main() are also redundant here, and can be omitted.
Anyway with the version of Dev-C++ that I believe comes with the book and assume you are using, the code will compile simply by changing <string.h> to <string>. However, the code will not be portable (and won't work with the latest Dev-C++ version): ISO standard C++ code without redundant headers and parameters would look like this:
include <string>
include <iostream>
using namespace std ;
int main()
{
string mystring;
mystring= "Hello there";
cout<<mystring<<endl;
Now you will probably then encounter the most common problem encountered by the unwary. In Windows, when a process completes, the OS closes its window. So unless you run this code from a command line console so that it does not have its own window, you will never see the output. you need to stop the window from closing. The brutal but reliable way is to invoke the system pause command thus:
Note the re-inclusion of stdlib (in its non-deprecated form) - that is where the system() function is declared. Actually to be completely portable you should have: std::system( "pause" ), but this implementation copies the C library to the std:: namespace so system() exists in both the global and std namespaces.
The latest version of Dev-C++ will complain if you use the deprecated C++ headers, but quietly allows the .h forms of the C library headers. The .h forms of the C headers are not identical to the C++ forms, the C++ compliant versions in many cases include additional function overloads so for example sqrt() in <cmath> works with float, double, and long double types without having special named versions.
Google on "C++ namespaces" and "deprecated headers" to get yourself up-to-speed. For the time being it is probably sufficient to 'convert' the dummies code using the non-deprecated headers and declaring "using namespace std;" after the includes. In the long term, and on larger or more complex projects, this may not be a viable practice, and will catch you out in the end in ways that will be hard to diagnose.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
so i've bought my self a book to teach my self C++ (c++ for dummies) and it shows me the following code to enter:
include <stdio.h>
include <stdlib.h>
include <string.h>
include <iostream.h>
int main(int argc, char *argv[])
{
string mystring;
mystring= "Hello there";
cout<<mystring<<endl;
return 0;
}
this SHOULD print out "Hello there".. (or says the book...and other sources ive found over the internet)
but i get the following errors:
8 string_cpp.cpp
`string' undeclared (first use this function)
and then bunch of errors
The book is wrong and so are any other sources you have found that suggest that this is correct. The book is not only wrong but very out of date. I believe that it also comes with an out of date version of Dev-C++ (but it seems not so out-of-date that the code in the book works - if it ever did!).
Most of what the book teaches can be 'fixed' by learning about deprecated header forms and namespaces (but not if you are using the version that came with the book perhaps - you'll need v4.9.9.2). If you want an example of a working C++ "hello, world" try the template project provided by Dev-C++ (File->New->Project->Introduction).
So you should do the following:
1) Read the "PLEASE READ BEFORE POSTING A QUESTION" thread on this forum.
2) The thread in (1) tells you how to uninstall your old version and install the new version in a way that won't cause conflicts.(it is not as simple as you might like!). You should do that - not least because it is hard for us to support you with a long abandoned version that none of us have installed.
3) Read the rest of that thread it tells you many things you will need to know, including:
--a) How to post questions on the forum (version number, OS, compile log, as well as code)
--b) How to stop the program terminating and closing the window before you see the output
Anyway the fundamental problem with your code is that <string.h> is the C string library header not the C++ string class header. That is simply <string> with no .h. In C++ all the .h forms are deprecated for standard library headers. C library headers acquire a 'c' prefix, allowing <string> to be distinct from <cstring> (equivalent to the old <string.h>)
It is anyone's guess why the code from the book includes <stdlib.h> or <stdio.h> since the code does not use anything from those headers. If this is indicative of the quality of the whole book, you need to proceed cautiously. The arguments to main() are also redundant here, and can be omitted.
Anyway with the version of Dev-C++ that I believe comes with the book and assume you are using, the code will compile simply by changing <string.h> to <string>. However, the code will not be portable (and won't work with the latest Dev-C++ version): ISO standard C++ code without redundant headers and parameters would look like this:
include <string>
include <iostream>
using namespace std ;
int main()
{
string mystring;
mystring= "Hello there";
cout<<mystring<<endl;
return 0;
}
or alternatively
include <string>
include <iostream>
int main()
{
std::string mystring;
mystring= "Hello there";
std::cout << mystring << std::endl;
return 0;
}
Now you will probably then encounter the most common problem encountered by the unwary. In Windows, when a process completes, the OS closes its window. So unless you run this code from a command line console so that it does not have its own window, you will never see the output. you need to stop the window from closing. The brutal but reliable way is to invoke the system pause command thus:
include <cstdlib>
include <string>
include <iostream>
int main()
{
std::string mystring;
mystring= "Hello there";
std::cout << mystring << std::endl;
system( "pause" ) ;
return 0;
}
Note the re-inclusion of stdlib (in its non-deprecated form) - that is where the system() function is declared. Actually to be completely portable you should have: std::system( "pause" ), but this implementation copies the C library to the std:: namespace so system() exists in both the global and std namespaces.
The latest version of Dev-C++ will complain if you use the deprecated C++ headers, but quietly allows the .h forms of the C library headers. The .h forms of the C headers are not identical to the C++ forms, the C++ compliant versions in many cases include additional function overloads so for example sqrt() in <cmath> works with float, double, and long double types without having special named versions.
Google on "C++ namespaces" and "deprecated headers" to get yourself up-to-speed. For the time being it is probably sufficient to 'convert' the dummies code using the non-deprecated headers and declaring "using namespace std;" after the includes. In the long term, and on larger or more complex projects, this may not be a viable practice, and will catch you out in the end in ways that will be hard to diagnose.
Clifford