C++/Im new, and am very confused
Im in the process of learning c++ from a book i have, Im using Dev-c++ 4.9.9. Win xp pro
1. Whats the deal with, #include <iostream.h> It dont work, it only worlks if i do #include <iostream>, and then put using namespace std;
2.I have seen this written several times clrscr();, i assume it clears the screen, but it doesnt work, i get the error 'clrscr' undeclared (first use this function)
3. And finally. This is really bothering me, i get some programs off the web, its helps me learn, but i see, always people use void, indtead of int for the main function, like this
the book says to use int main(), and also, sometimes in the braces to put ( int argc, char *argv) argc.
I dont get it, when i try to compile using the void methode, i get the error 'main' must return int.
I am very troubled, i got all these programs from reliable sources, maybe i am confusing something with the old c language, i dont kno;w, but the progs should work, and they just dont, i have to always change th code around,
I have also tried to compile with gcc g++, same thing.
Thanks, for all you help, i appreciate it a lot.
1/ The C++ ISO/ANSI standard library header file is iostream. Before there was a standard for C++ the 'traditional' iostream library used a header called iostream.h. The lack of the .h extension is common throughout _all_ C++ standard library header files. Those that map onto C header library headers are prefixed with a c as in cstdio or ctime. The namespace std is used for all C++ names in the standard library - including those included by using the c<name> format for C header files such as cstdio. This again is a departure from the 'traditional' pre standard state of affairs.
Your code will not only work by placing using namespace std before using any such names (after all header files have been included though). You can also bring the names in one at a time e.g. using std::cout. Or you can fully qualify the name - that is you can specify std in the names thus: std::cout, std::endl, etc... I would suggest that your book is out of date.
2/ clrscr is _not_ any part of standard C++. C++ has no graphic functionality built in as standard. If you do have a library for your platform that provides such a function then you will have to look up in the documentation for the library what header file(s) to include in your code before using it and what library to link your code against to build an executable from it.
As far as I can tell this is in fact a Microsoft 16-bit C/C++ function for clearing the screen of the _terminal_ in DOS. It resided in the (MS specific) header file conio.h. For other MS platforms the simplest suggestion is to use system("cls"); for Linux and similar platforms using the bash shell you could try system( "clear" ); system is a C (and C++) library function. Include <cstdlib> (system should be in namespace std) or <stdlib.h> (system should be in the global (unnamed) namespace). Again your book seems out of date, and MS and DOS specific to boot.
3/ In this respect the book is correct. The only two standard conforming signatures for the C++ main entry point are:
int main(int argc, char* argv)
(taken from the C++ standard itself, section 3.6.1 paragraph 2)
The other odd thing about main is that it is the only function declared to return a value that you do not have to return a value from - not doing so is taken by the compiler to mean you meant to return 0 - which it does for you. Note that the return value from main is passed to the function exit (declared in cstdlib) which passes this value back to the execution environment (e.g. a command shell). I think this behaviour is again a hang over from long standing practice, maybe from the earliest days of C (but don't quote me on that - I'm not sure!).
Your (and others) problem with code using void main() is that this was until recently a non-standard feature of Microsoft Visual C++ (MS VC++). In fact because of these non-standard MS compilers many people actually think void main() is a correct way to declare main.
Those in the know that have had to please MS VC++ and more conformant compilers always used int main and returned a value - even if it was just the return 0 the compiler should have supplied for us!
I would be careful with any programs that you get that do use void main - this is a warning flag that you are entering the world of (older) MS VC++ code which is not always the best example - not just because of its incorrect signature for main. Use them to learn but beware that older MS VC++ code (i.e. code written for MS VC6 or earlier) has problems with some parts of the language and all MS C++ compilers have loads of MS specific features such as special keywords to support COM and for the newer .NET compiler versions reams of support for .NET managed code (which is about to become obsolete as MS with others are creating a newer, cleaner, standard for C++/.NET interoperability).
Note that MS are not the only ones who have limited support, compliance or defects – I seem to remember that Sun also had limitation with their C++ compilers (different from those MS has of course - just to make life interesting for those of us writing code for both compilers types!). In most cases newer compilers have better support although most compilers do not have 100% language support and standards conformance.
However most such problems - other than the ones you have encountered - should not hit you for a while yet.
I would suggest you get yourself a much more up to date text on C++. You can look at the book reviews and other developer resource links at the ACCU web site - http://www.accu.org/
Hope this helps a little.