My Hello World failed!

A

Anil Gupte

I have never been so embarassed! :) I have played with C++, and even
created a few simple programs, but decided to learn it properly. So I crack
a book, get into Visual Studio and follow the instructions to create the
following program:

// HelloWorld.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}

and I get an error:

c:\Projects\CPlus2\HelloWorld\HelloWorld.cpp(12): fatal error C1010:
unexpected end of file while looking for precompiled header directive

The code is exactly as shown in the book - (Visual C++ Step by Step by
Templeman and Olsen). What gives?
 
C

Carl Daniel [VC++ MVP]

Anil said:
I have never been so embarassed! :) I have played with C++, and even
created a few simple programs, but decided to learn it properly. So
I crack a book, get into Visual Studio and follow the instructions to
create the following program:

// HelloWorld.cpp : Defines the entry point for the console
application. //
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}

and I get an error:

c:\Projects\CPlus2\HelloWorld\HelloWorld.cpp(12): fatal error C1010:
unexpected end of file while looking for precompiled header directive

The code is exactly as shown in the book - (Visual C++ Step by Step by
Templeman and Olsen). What gives?

Add

#include "stdafx.h" as the first non-comment line in your .cpp file.

OR

Right-click on your .cpp file in the soluition explorer, choose
"Properties", and find the "Precompiled header files" option - change it to
"not using pre-compiled headers".

-cd
 
A

Anil Gupte

Thanx for the response. Since I am learning VC++ (I am familiar with
VB.Net), I would appreciate it if you can tell me what that file was for and
why do you think the author forgot to mention it?
 
B

Bo Persson

Anil Gupte wrote:
:: Thanx for the response. Since I am learning VC++ (I am familiar
:: with VB.Net), I would appreciate it if you can tell me what that
:: file was for and why do you think the author forgot to mention it?

I don't think the author forgot to mention this file. This is VC
specific.

When you create a new project, you get the Precompiled Header option
checked by default. The files generated by the project wizard will
also contains an #include "stdafx.h".

If you want to use your own code, uncheck the Precompiled Header and
check the Empty Project option instead. Then add any files you like.


Bo Persson


::
:: --
:: Anil Gupte
:: www.keeninc.net
:: www.icinema.com
::
:: "Carl Daniel [VC++ MVP]"
:: ::: Anil Gupte wrote:
:::: I have never been so embarassed! :) I have played with C++,
:::: and even created a few simple programs, but decided to learn it
:::: properly. So I crack a book, get into Visual Studio and follow
:::: the instructions to create the following program:
::::
:::: // HelloWorld.cpp : Defines the entry point for the console
:::: application. //
:::: #include <iostream>
:::: using namespace std;
:::: int main()
:::: {
:::: cout << "Hello World!" << endl;
:::: return 0;
:::: }
::::
:::: and I get an error:
::::
:::: c:\Projects\CPlus2\HelloWorld\HelloWorld.cpp(12): fatal error
:::: C1010: unexpected end of file while looking for precompiled
:::: header directive
::::
:::: The code is exactly as shown in the book - (Visual C++ Step by
:::: Step by Templeman and Olsen). What gives?
:::
::: Add
:::
::: #include "stdafx.h" as the first non-comment line in your .cpp
::: file.
:::
::: OR
:::
::: Right-click on your .cpp file in the soluition explorer, choose
::: "Properties", and find the "Precompiled header files" option -
::: change it to "not using pre-compiled headers".
:::
::: -cd
 
B

Ben Voigt [C++ MVP]

Bo Persson said:
Anil Gupte wrote:
:: Thanx for the response. Since I am learning VC++ (I am familiar
:: with VB.Net), I would appreciate it if you can tell me what that
:: file was for and why do you think the author forgot to mention it?

I don't think the author forgot to mention this file. This is VC specific.

The name of the book the OP was using started with "Visual C++", so that
would seem to be a rather serious oversight. Probably the new project
wizard has changed since the book was written, so it wasn't clear to the OP
that the "Empty Project" option needed to be checked.
When you create a new project, you get the Precompiled Header option
checked by default. The files generated by the project wizard will also
contains an #include "stdafx.h".

If you want to use your own code, uncheck the Precompiled Header and check
the Empty Project option instead. Then add any files you like.


Bo Persson


::
:: --
:: Anil Gupte
:: www.keeninc.net
:: www.icinema.com
::
:: "Carl Daniel [VC++ MVP]"
:: ::: Anil Gupte wrote:
:::: I have never been so embarassed! :) I have played with C++,
:::: and even created a few simple programs, but decided to learn it
:::: properly. So I crack a book, get into Visual Studio and follow
:::: the instructions to create the following program:
::::
:::: // HelloWorld.cpp : Defines the entry point for the console
:::: application. //
:::: #include <iostream>
:::: using namespace std;
:::: int main()
:::: {
:::: cout << "Hello World!" << endl;
:::: return 0;
:::: }
::::
:::: and I get an error:
::::
:::: c:\Projects\CPlus2\HelloWorld\HelloWorld.cpp(12): fatal error
:::: C1010: unexpected end of file while looking for precompiled
:::: header directive
::::
:::: The code is exactly as shown in the book - (Visual C++ Step by
:::: Step by Templeman and Olsen). What gives?
:::
::: Add
:::
::: #include "stdafx.h" as the first non-comment line in your .cpp
::: file.
:::
::: OR
:::
::: Right-click on your .cpp file in the soluition explorer, choose
::: "Properties", and find the "Precompiled header files" option -
::: change it to "not using pre-compiled headers".
:::
::: -cd
 
P

pvdg42

Ben Voigt said:
The name of the book the OP was using started with "Visual C++", so that
would seem to be a rather serious oversight. Probably the new project
wizard has changed since the book was written, so it wasn't clear to the
OP that the "Empty Project" option needed to be checked.
I'd like to see that book, as the oversight covers previous versions of
Visual Studio going back to VS 6. Our beginning C++ students are instructed
to *always* create empty Win32 console projects, then add a source code
file. We've used that procedure since VC 6 to avois the issue experienced by
the OP.
Possibly the OP missed the Empty Project step in the book?
 
B

Ben Voigt [C++ MVP]

pvdg42 said:
I'd like to see that book, as the oversight covers previous versions of
Visual Studio going back to VS 6. Our beginning C++ students are
instructed to *always* create empty Win32 console projects, then add a
source code file. We've used that procedure since VC 6 to avois the issue
experienced by the OP.
Possibly the OP missed the Empty Project step in the book?

IIRC, no precompiled headers was the default in prior versions of VC, you
had to request skeleton code.
 
P

PvdG42

Ben Voigt said:
IIRC, no precompiled headers was the default in prior versions of VC, you
had to request skeleton code.
I have no installations of VS 2003 or earlier to help me remember, but we
instructed students to create empty projects in versions earlier than VS
2005 to avoid entanglements with some sort of code shell, I thought. Could
be my memory is bad, though :)
 
A

Anil Gupte

Sorry, I have been away.

Yes, you are absolutely right - Mea Culpa. I overlooked the "Check the
Empty Project box" instruciton. It was at the top of the page and I just
glazed....

:)

Thanx all!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top