0

I tried doing a simple hello world in C++ as I'm going to be using it in school in about a week. Why can't I compile this properly?

c:\Users\user\Desktop>cl ram.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

ram.cpp
ram.cpp(1) : fatal error C1083: Cannot open include file: 'iostream.h': No such
file or directory

c:\Users\user\Desktop>

Here is ram.cpp

#include <iostream>

int main()
{
    cout << "Hello World!";
    return 0;
}

EDIT:

I updated my code to

#include <iostream>
using namespace std;

int main(void)
{
    cout << "Hello World!";
    return 0;
}

And still get this error

ram.cpp
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : wa
rning C4530: C++ exception handler used, but unwind semantics are not enabled. S
pecify /EHsc
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:ram.exe
ram.obj
netbyte
  • 11
  • 3

3 Answers3

9

The compiler told you why:

ram.cpp(1) : fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory

You don't use the .h. Just use

#include <iostream>

A long winded explanation with a lot of background can be found here.

Per your comment, you need to buy a new book. Yours is so woefully outdated that it does not even mention namespaces! To get your program working, try this:

#include <iostream>

int main()
{
    std::cout << "Hello World!";
    return 0;
}

cout lives in the std namespace.

If it becomes cumbersome to type std:: often then you can import a type for the whole file like so:

using std::cout;

And now you can just write cout instead. You can also import the entire namespace, but this is generally bad practice because you are pulling the entire thing into the global namespace, and you may run into collisions down the road. However, if this is something you know will not be an issue (for example, in a throwaway app or a small utility) then you can use this line:

using namespace std;
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • Thanks for the link to the explanation – antlersoft Mar 22 '12 at 22:02
  • Thanks. But now I get this error c:\Users\alex\Desktop>cl ram.cpp Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved. ram.cpp C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : wa rning C4530: C++ exception handler used, but unwind semantics are not enabled. S pecify /EHsc ram.cpp(4) : error C4430: missing type specifier - int assumed. Note: C++ does n ot support default-int ram.cpp(5) : error C2065: 'cout' : undeclared identifier – netbyte Mar 22 '12 at 22:11
  • 1
    @netbyte : `cout` is in namespace `std`. What ever book you're reading that is showing you the old headers and non-namespaced symbols, drop it. Burn it. [Get a better book](http://stackoverflow.com/q/388242/636019). – ildjarn Mar 22 '12 at 22:18
  • @ildjarn Thank you! Now how exactly can I include this? – netbyte Mar 22 '12 at 22:19
  • @netbyte : Read a book that isn't so ancient that it doesn't mention namespaces. ;-] – ildjarn Mar 22 '12 at 22:20
  • @EdS. is this possible without calling std every time? – netbyte Mar 22 '12 at 23:02
  • @netbyte: You can, I added another example. – Ed S. Mar 22 '12 at 23:18
5

It's not called "iostream.h", and it never was. Use #include <iostream>.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

The proper code should be

#include <iostream>

int main()
{
    std::cout << "Hello World!" << std::endl;
    return 0;
}

Whatever book had been written before 2003, is not aware of this. Just throw it away. The world had moved somewhere else in the meantime!

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63