compiler problem

Hey, I have been using java for a long time but I am currently learning C++ so.  When I try to compile this simple code in Microsoft.net edition

#include <iostream>
using namespace std;

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

i get a fatal error: "unexpected end of file while looking for precompiled header directive"
yattiasAsked:
Who is Participating?
 
jkrCommented:
Try

#include "stdafx.h"
#include <iostream>
using namespace std;

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

and provide an empty "stdafx.h" in your current directory. VC++ used that file to create so called "precompiled headers" (which speed up the compilation process) and sets that file for the default. You could also turn that of in your project settings instead, go to "C++", choose "precompiled headers" and disable that.
0
 
yattiasAuthor Commented:
thanks, it works now.  what is that "stdafx.h" and what does it do? how come the console opens and closes right away when I try to run it?
0
 
jkrCommented:
>>what is that "stdafx.h" and what does it do?

As I wrote, VC++ uses it as its base file for precompiling headers. That has nothing to do with C++, but is a special thing of VisualStudio

>>how come the console opens and closes right away when I try to run it?

When your program terminates, the console will close. Just use

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
     char c;
     cout << "Hello" << endl;
     cin >> c;
     return 0;
}

(type a single char and hit "Enter" to terminate it)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.