73

I am trying to compile a simple VS program in C++ as an assignment for class. We only ever include <iostream> and I keep getting this error:

1>Assignment.cpp(15): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?

My program is literally this small...

#include <iostream>
using namespace std;
int main()
{
    unsigned int day = 30;

    cout << "My Name is John Doe" << endl;
    cout << "My Major is CS" << endl;
    cout << "I was born on day " << day << endl;
    return 0;
}

I just installed Visual Studio Express 2010. Really I would love to start an empty project instead of installing with all these files predefined, I think it would make it a lot easier but I never get that option when creating a project. Anybody have any suggestions?

Howdy_McGee
  • 10,422
  • 29
  • 111
  • 186

4 Answers4

102

You can always disable the use of pre-compiled headers in the project settings.

Instructions for VS 2010 (should be similar for other versions of VS):

Select your project, use the "Project -> Properties" menu and go to the "Configuration Properties -> C/C++ -> Precompiled Headers" section, then change the "Precompiled Header" setting to "Not Using Precompiled Headers" option.


If you are only trying to setup a minimal Visual Studio project for simple C++ command-line programs (such as those developed in introductory C++ programming classes), you can create an empty C++ project.

André Caron
  • 44,541
  • 12
  • 67
  • 125
  • See, I tried this but then I do not get the "Press Enter To Continue" By default when I print out strings. It is fairly annoying. – Howdy_McGee Aug 31 '11 at 18:39
  • 2
    @Howdy_McGee `std::cout << "Press Enter To Continue" << std::endl; std::cin.get();` should get you there. – Tom Kerr Aug 31 '11 at 18:41
  • Is there a way I can get that to happen by default? In class we never need to add any of that extra code to get the press enter to continue string. – Howdy_McGee Aug 31 '11 at 18:44
  • You can press CTRL+F5, but it won't allow you to debug code. It's called "Start Without Debugging". – Marlon Aug 31 '11 at 18:45
  • See that's is exactly what I thought, but doing this in an empty project still exits out of cmd prompt before I can actually see the strings I've printed. Doing the ctrl + f5 that is - even in debug mode it does not give me the "press enter to continue" option – Howdy_McGee Aug 31 '11 at 18:46
  • @Howdy: The proper way to handle that is to execute your program in a persistent console window. It is not the job of your program to compensate for your having used a temporary console window, by arbitrarily waiting for user input that it never uses. – Lightness Races in Orbit Aug 31 '11 at 18:51
  • Setting Linker -> System to Console should display the message again. – user168715 Aug 31 '11 at 18:53
  • @Tom K: note that `std::cin.get()` is not required to block on console input. I know that at least on GCC, it returns `-1` if no input is available. – André Caron Aug 31 '11 at 18:58
  • -1 What do precompiled headers have to do with standards compliance? – ildjarn Aug 31 '11 at 19:45
  • @ildjarn: Nothing. However, all other C++ project types in Visual Studio require some non-standard entry point (`wMain`, `WinMain`, ...). The empty C++ project is the only one that will compile a small, self-contained, standard C++ command-line program such as the one posted by OP. – André Caron Aug 31 '11 at 19:50
  • 1
    @André : The 'Win32 Project' and 'Win32 Console Application' templates also give the option of disabling precompiled headers. In any case, my point was that since his issue is related to precompiled headers, and precompiled headers are orthogonal with standards compliance, it's misleading to bring up standards compliance at all. – ildjarn Aug 31 '11 at 19:56
  • 2
    @ildjarn: but the issue is not about pre-compiled headers. The real issue is about OP having selected the wrong project type and/or options in the project wizard dialog. Since OP is writing school assignments in what seems to be plain, standard C++, the project type that best helps develop good habits for writing standard compliant C++ programs in Visual Studio is the empty project type. Hence, my mention of standards compliance. – André Caron Aug 31 '11 at 20:01
  • 2
    @André: `traits::eof()` really, not `-1`. – Lightness Races in Orbit Aug 31 '11 at 20:09
  • @ildjarn: re-worded to remove mention of "standards compliance". – André Caron Sep 04 '11 at 18:28
  • You say that I can disable the precompiled header, but somewhat you don't care to say how. -1 – Tomáš Zato Jun 18 '14 at 22:44
  • @TomášZato: Added instructions for VS2010. Next time, you can simply ask instead of downvoting... – André Caron Jun 23 '14 at 17:00
  • 1
    Downovote's tooltip says: "*This answer is not useful*". That's, of course, relative. As well, downvote can be reverted any time. Thank you for improving the answer. – Tomáš Zato Jun 23 '14 at 17:11
9

You can create an empty project by selecting the "Empty Project" from the "General" group of Visual C++ projects (maybe that project template isn't included in Express?).

To fix the problem in the project you already have, open the project properties and navigate to:

Configuration Properties | C/C++ | Precompiled Headers

And choose "Not using Precompiled Headers" for the "Precompiled Header" option.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • According to http://msdn.microsoft.com/en-us/library/ms235516%28v=VS.100%29.aspx, the Empty Project template is in Express. Let me know if it doesn't show up for you. I find it preferable to any of the other project templates for setting up a quick-n-dirty, small, self-contained test project. – Michael Burr Aug 31 '11 at 18:42
6

The .cpp file is configured to use precompiled header, therefore it must be included first (before iostream). For Visual Studio, it's name is usually "stdafx.h".

If there are no stdafx* files in your project, you need to go to this file's options and set it as “Not using precompiled headers”.

hamstergene
  • 24,039
  • 5
  • 57
  • 72
-1

try to add #include "stdafx.h" before #include "iostream"

axel22
  • 32,045
  • 9
  • 125
  • 137
SergeyT
  • 65
  • 1
  • 8
  • It's a class assignment - I'm only allowed to use iostream so this solution will not work – Howdy_McGee Aug 31 '11 at 18:39
  • 1
    @Howdy: Well technically Sergey is correct; you have created a project set to use a precompiled header, yet you don't `#include` it. What you really need to do is create an empty project or remove the reliance on pre-compiled headers. – Ed S. Aug 31 '11 at 18:41
  • @Ed S.: although it is correct, it only fixes the symptom. The hidden question is "why can't I compile the same program I compiled in class"? And the answer to *that* question is "in class, the project didn't use a pre-compiled header". – André Caron Aug 31 '11 at 19:26