2

I am a beginner in C++, and I am here to learn.

First of all, I made some programs in Borland C++, at school, but my school doesn't have Visual C++, and I don't have anybody to teach me how to program in Visual C++.

The problem is that when I try to change the linker subsystem (project settings) to Windows (/SUBSYSTEM:WINDOWS), I get this in output window:

1>------ Build started: Project: hew, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\users\mxmike\documents\visual studio 2010\projects\hew\main.cpp(1): fatal
error C1083: Cannot open include file: 'iostream.h': No such file or directory
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

My code is really simple:

#include <iostream.h>
#include <stdlib.h>

int main(int f)
{
    int i=1;
    return 1;
}

I simply don't get it. Would someone be so kind do explain to me?
Thank you for reading!

Johan
  • 74,508
  • 24
  • 191
  • 319
Michael
  • 23
  • 2
  • 1
    You should `#include ` and `` (without `.h`) – Andy Prowl Apr 04 '13 at 20:34
  • 1
    Or not mention std lib.h, in whichever flavor, since nothing it declares is used. That way I won't have to point out that stdlib.h works just fine. – Pete Becker Apr 04 '13 at 20:36
  • You should be returning 0 on success as well. `main()` will do that for you if you leave it out. I'll be surprised if your compiler supports that signature, too. It's only required to support `int main()` and `int main(int, char**)`. – chris Apr 04 '13 at 20:37

2 Answers2

2

There is no <iostream.h> header. The standard library header for I/O is <iostream>. None of the C++ standard library headers end with .h.

The headers that do exist that end with .h are from the C standard library. So, for example, <stdlib.h> is a C standard library header. The C++ standard does make these headers available, but it also provides its own alternatives with almost identical contents. Simply remove the .h and add a c to the beginning. So the C++ version of <stdlib.h> is <cstdlib>.

Whether you actually need the contents of either <stdlib.h> or <cstdlib> is a different matter. Most of the functionality has improved C++ counterparts in C++-specific headers. For example, these C headers provide malloc, but you should instead be using new-expressions in C++.

Also note that returning 1 from main is typically a sign of failure. To indicate a successful execution, do return 0; instead.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
1

There are two standard types of header files in C++. Those that derive from C, such as < stdlib.h > which in C++ should be included as < cstdlib > (take off the .h and prefix with a c) and those like < iostream > which is a C++ header file which replaces the C < stdio.h >.

What you want is:

#include <cstdio>
#include <cstdlib>

or

#include <iostream>
#include <cstdlib>

depending on which functionality/functions you call in your code (in the case you supply none so both should work).

Regards,

Jason Posit

Jason Posit
  • 1,323
  • 1
  • 16
  • 36
  • @PeteBecker, Sure they work, but it's best to use the C++ version and `std::xxx`. – chris Apr 04 '13 at 20:40
  • @chris - what benefit do you see from using the C++ versions? Other than not having to point out that they provide exactly the same functions... – Pete Becker Apr 04 '13 at 20:42
  • @PeteBecker, I'm trying to find the question about it that I remember seeing. – chris Apr 04 '13 at 20:50
  • @PeteBecker, Looks like it isn't as good as I remembered: http://stackoverflow.com/questions/5079325/should-i-include-stddef-h-or-cstddef – chris Apr 04 '13 at 20:56
  • @chris - ``. The reasons given for insisting on the C++ versions are usually overinflated personal preferences. There is no sound technical reason for choosing either way (especially now that C++11 has recognized reality and removed the unenforceable requirement that the C++ headers not dump the C names into the global namespace). – Pete Becker Apr 04 '13 at 21:03
  • @PeteBecker, It's a real shame we have two versions of them then. At least modules are a proper solution. They fix compilation times, they fix macros, and they fix polluting. – chris Apr 04 '13 at 21:14
  • @chris - the current scheme is the result of wishful thinking on the part of some members of the standards committee the first time around. – Pete Becker Apr 04 '13 at 23:21
  • @PeteBecker It was a nice thought at the time, but it ended up being totally impractical. – chris Apr 04 '13 at 23:23