-1
#include <iostream>
using namespace std;

int Main ()
{
    cout << "---------------------------------------------------------------------------------/n";
    cout << "NAME: Justin Chiang/n";
    cout << "COMPUTER LANGUAGES: Python, Lua, C++/n";
    cout << "FAVORITE VIDEO GAME: Starcraft, Diablo/n";
    cout << "I'm taking this class for math undergrad requirement and because it is very useful." << endl;
    system("PAUSE");
    return 0;
}

OUTPUT:

1>------ Build started: Project: HW1, Configuration: Debug Win32 ------
1>  HW1.cpp
1>c:\users\asus\desktop\hw1\hw1\hw1.cpp(5): warning C4627: '#include <iostream>': 
            skipped when looking for precompiled header use
1>          Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\asus\desktop\hw1\hw1\hw1.cpp(25): fatal error C1010: unexpected end of file 
            while looking for precompiled header. Did you forget to add '#include  
            "StdAfx.h"' to your source?
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Justin Chiang
  • 175
  • 1
  • 1
  • 8
  • 1
    `Did you forget to add '#include "StdAfx.h"' to your source?` Troubleshooting a build failure couldn't possibly get any easier! – Praetorian Sep 10 '12 at 05:47
  • @Prætorian So where in the C++ standard does it say that every C++ program must begin with `#include "stdafx.h"`? – john Sep 10 '12 at 05:48
  • 1
    Please don't develop the habit of putting [system("pause")](http://www.gidnetwork.com/b-61.html) at the end of every program you write. It's non-portable and just makes your program less useful. – David Schwartz Sep 10 '12 at 05:51
  • @john read [this](http://stackoverflow.com/questions/903228/why-use-precompiled-headers-c-c) on why you might possibly use a precompiled header. – Rapptz Sep 10 '12 at 05:53
  • 1
    @Rapptz I'm sure it's a good read, and I have nothing against precompiled headers at all. But I'm not seeing any indication that the OP is trying to use precompiled headers and failing. He's just created his first ever program and is getting a stupid error message about something he's never heard of. – john Sep 10 '12 at 05:57
  • 3
    @john Unless the OP went about creating his project file by hand, VisualStudio would've asked him if he wanted to use precompiled headers, which he didn't uncheck. So, instead of trying to get the OP to share your apparent dislike of precompiled headers, assume for a minute he knew what he was doing and answer his question! – Praetorian Sep 10 '12 at 05:57
  • 1
    @Prætorian You could be right, but I seriously doubt it. Much more likely he didn't know what the question meant and just accepted the default. Anyway the OP has all the information he needs now. He can pick what ever advice he thinks best. – john Sep 10 '12 at 05:59
  • What's with the `/n`? It's `\n` for new line. – quantum Oct 06 '12 at 01:37

3 Answers3

3

Go to project properties. Click on Configuration Properties/C/C++/Precompiled Header and pick the 'Not Using Precompiled Headers' option. The precise instructions might vary depending on exactly which version of Visual Studio you are using but basically you want to turn off precompiled headers.

john
  • 85,011
  • 4
  • 57
  • 81
2

You're missing #include "stdafx.h"

Also, int Main() isn't standard C++, you should replace your main function with int main() or int main(int argc, char** argv)

Since I have to say why you have to use a precompiled header, it makes compilation times faster with MSVC but it hinders portability and drags a lot of nonstandard conforming code if used. It's possible that it ruins your portability but you could easily just remove the header file if you're working with another IDE.

Rapptz
  • 20,807
  • 5
  • 72
  • 86
  • Which must be included before all other headers – Praetorian Sep 10 '12 at 05:44
  • 2
    He's not 'missing stdafx.h', he written a perfectly good C++ program which Visual Studio is refusing to compile because he has inadvertently turned on some non-standard options in the compiler. – john Sep 10 '12 at 05:46
  • 2
    @john `int Main() { ... }` is not a perfectly good C++ program – Praetorian Sep 10 '12 at 05:48
  • 1
    @john is `int Main()` perfectly good C++? It isn't standard, at the very least. – juanchopanza Sep 10 '12 at 05:48
  • @Prætorian, OK lets assume that's a typo. In any case not the question he is asking about. – john Sep 10 '12 at 05:49
  • @john his question states "Why does this not compile", the answer is because of his error which states he has to include "stdafx.h". – Rapptz Sep 10 '12 at 05:50
  • @Rapptz Personally I wouldn't take programming advice from the literal interpretation of compiler error messages. VC++ seems particularly bad at giving poor advice this way. – john Sep 10 '12 at 05:53
  • what should I put instead of int main() then? I added #include "stdafx.h" now at the header.... – Justin Chiang Sep 10 '12 at 05:54
  • 1
    -1 advicing a beginner to include the IDE-specific `` file is the entirely wrong thing to do. – Cheers and hth. - Alf Sep 10 '12 at 05:55
  • @JustinChiang it's case sensitive. Does it compile now? – Rapptz Sep 10 '12 at 05:56
  • 1
    @Cheersandhth.-Alf if he had wanted to port this somewhere else nothing is stopping him from removing the header file before porting it. – Rapptz Sep 10 '12 at 06:00
  • @Rapptz what is the sense of including a header if it isn't actually needed by the code in question? – juanchopanza Sep 10 '12 at 06:10
  • @Rappts: advicing a beginner to include the IDE-specific `` file is the entirely wrong thing to do. since the OP's problem is that he doesn't know what's going on, that's stopping him from removing the header file. your answer does not nothing to help with that, and on the contrary it encourages totally unnecessary bad practice. – Cheers and hth. - Alf Sep 10 '12 at 06:10
  • @juanchopanza precompiled headers in Visual Studio provide a faster compilation time since C++ compile times in Visual Studio are sometimes very slow without them. – Rapptz Sep 10 '12 at 06:14
  • 1
    @Rapptz: most people correcting you here *know* all that. they also know that with msvc precompiled headers (1) are msvc-specific, (2) allows some non-standard code to compile, and (3) prevents some standard code from compiling. which apparently you don't know. why are you *arguing*. you should just be thanking people for correcting you. – Cheers and hth. - Alf Sep 10 '12 at 06:16
  • Then you can probably nuance your answer a bit, mention benefits/drawbacks of such an include. – juanchopanza Sep 10 '12 at 06:16
  • I found out the main problem! I did not start an empty project and it was pre-configured. Thank you for all who told me to check header configuration properties. – Justin Chiang Sep 10 '12 at 06:42
0

int Main () should be in lower case. Change it to int main ().

And

Change cout << "... /n"; to cout << "... \n";

#include <iostream>
using namespace std;

int main ()
{
    cout << "---------------------------------------------------------------------------------\n";
    cout << "NAME: Justin Chiang\n";
    cout << "COMPUTER LANGUAGES: Python, Lua, C++\n";
    cout << "FAVORITE VIDEO GAME: Starcraft, Diablo\n";
    cout << "I'm taking this class for math undergrad requirement and because it is very useful." << endl;
    system("PAUSE");
    return 0;
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28