You are here:

C++/Im new, and am very confused

Advertisement


Question
Hi,
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
void main()

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.  

Answer
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 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.
Happy coding...  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.