By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,230 Members | 2,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,230 IT Pros & Developers. It's quick & easy.

#include <iostream>, Precompiled headers error

P: 5
Hi there guys, I've recently purchased "Sam's Teach Yourself C++ Fifth Edition" (About a week before the 6th edition came out)

I'm trying to work through the book, but in trying to compile Hello World, I keep getting an error instead of it making the program.

I've looked around the internet for a reason, but come up stuck.

My only assumption would be that the fifth edition is upgraded to suit newer compilers? (I downloaded Visual C++ 2008 Express Edition from Microsoft.

I am in a Win32 console project, and enter the code as follows:

#include <iostream>

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

But when I try and compile, I get the following:

1>------ Build started: Project: HelloWorld, Configuration: Debug Win32 ------
1>Compiling...
1>HelloWorld.cpp
1>c:\documents and settings\adams\my documents\visual studio 2008\projects\helloworld\helloworld\helloworld.cpp (1) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\documents and settings\adams\my documents\visual studio 2008\projects\helloworld\helloworld\helloworld.cpp (8) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
1>Build log was saved at "file://c:\Documents and Settings\Adams\My Documents\Visual Studio 2008\Projects\HelloWorld\HelloWorld\Debug\BuildLog .htm"
1>HelloWorld - 1 error(s), 1 warning(s)

Sorry about the question, just wonderinfg if it's something I'm doing wrong, or if I need a new book, or to change something throughout the book?
Aug 10 '08 #1
Share this Question
Share on Google+
11 Replies


Expert 100+
P: 671
Sorry about the question, just wonderinfg if itís something Iím doing wrong, or if I need a new book, or to change something throughout the book?
The code you are compiling is correct. The issue is actually a bit tangential to your code. Thereís something called a precompiled header, and your IDE project is configured to use it. I wonít go into details now because itís something you need to stay away from.

Create a new C++ project. Make sure you select the following options when selecting what kind of project to make. It must be a console project. There is also an option (probably selected by default) for precompiled headers. Make sure precompiled headers if not selected. Create this new project, try to compile the same code there, and tell us how it goes.
Aug 10 '08 #2

P: 5
Thanks oler1s.

I created a new Solution and turned off the Precompiled headers, entered in the code again and it ran. Though flashed only very briefly haha. (The book I have has told me to add two more lines to wait for a character input if this happens.)

So I'll assume for each program in this book, I shouldn't be using Precompiled headers?
Aug 10 '08 #3

Banfa
Expert Mod 5K+
P: 8,916
Probably not a bad assumption until you understand what precompiled headers are and how to use them.

This is an interoperability problem between VC++ 2008 and you book examples.

The book examples assume that the code in the example is the only code, reasonably enough. However if you enable precompiled headers in VC++ 2008 it assumes you are creating your precompiled headers through a header call stdafx.h even though any header could be used.

Since stdafx.h is not in the book examples you get an error.

Anyway, keep precompiled headers switch off, it is unlikely any of the book examples will be big enough that they would make any difference to compilation time. Precompiled headers are a way of speeding up compilation on a large project.


On another point if your program flashed through quickly then you probably ran it by pressing F5, that is ran it in the debugger. This is not running it properly and the debugger assumes that if you want it to stop you will have set a break-point.

Try instead <Ctrl>-F5 this tells the IDE to execute the program normally and it should stop at the end of execution with a "Press any key to continue . . ." message.

You should not be adding lines of code to pause any program when its execution has finished this is extremely poor practice, not required because the system does it for you if you use it properly and prevents your program from being run by another program.
Aug 10 '08 #4

P: 5
Try instead <Ctrl>-F5 this tells the IDE to execute the program normally and it should stop at the end of execution with a "Press any key to continue . . ." message.
I gave <Ctrl> F5 a go, and it worked.

The book had told me to add:

char response;
cin >> response;

Before the return 0;

But I'll go this way instead, if it's not a good habit to get in. The debugger then isn't specifically running your program for you? Just running through it to make sure it will when you need to, correct?

Thanks for all your help.
Aug 10 '08 #5

Banfa
Expert Mod 5K+
P: 8,916
But I'll go this way instead, if it's not a good habit to get in. The debugger then isn't specifically running your program for you? Just running through it to make sure it will when you need to, correct?
Not quite when you use <Ctrl> F5 the debugger is not involved at all, however if a program has debugging information in it and crashes on a system with a debugger installed (VC++ 2008 for instance) then you will be asked if you want to perform JIT debugging. It doesn't matter too much how the program was run, i.e. <Ctrl> F5 double click from explorer Start->Run...
Aug 10 '08 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
It seems the OP has difficulty using Visual Studio.NET.

First, create the correct project:
1) Create a Win32 project
2) When the wizard appears DO NOT click finish.
3) Instead, click Application Settings
4) Select console application and empty project
5) Then click finish.

This removes all of the Microsoft-specific stuff like pre-compiled headers and that pesky stdafx.h file.

Then, code and build your program.

Then, to execute, select Start Without Debugging rather than simply Start.

Visual Studio (by default) creates a debug project. That is, it puts stuff in the .exe to support the debugger. When you Start, Visual Studio assumes you have pre-set breakpoints to pause your code. If you don't have these preset breakpoints all you see is a black flash as your program goes by.

Instead, select Start Without Debugging. In this case you are telling Visual Studio that while this is a debug program, you are not using your debugger. e, Visual Studio will now pause execution at the end of main() with "press any key to continue...".

You should not need any silly code at the end of main() to pause the program during debugging. Maybe some other not so friendly compilers require this but not Visual Studio.
Aug 10 '08 #7

P: 5
You should not need any silly code at the end of main() to pause the program during debugging. Maybe some other not so friendly compilers require this but not Visual Studio.
Ok, so it runs fine in the compiler, but when I build a project and run it outside of Visual C++, it simply runs through the program until no more input is needed, then exits. Should I put in the code to stop it doing this now? Or am I doing something wrong?
Aug 15 '08 #8

Banfa
Expert Mod 5K+
P: 8,916
Ok, so it runs fine in the compiler, but when I build a project and run it outside of Visual C++, it simply runs through the program until no more input is needed, then exits. Should I put in the code to stop it doing this now? Or am I doing something wrong?
How do you run it? Double click in explorer? try starting a command prompt and running it (very easy if you have the Command Promt Here power tool installed).

I would still say no, do not add code to pause it at the end, if you need the output then run from a command prompt, if you put that pause in you are effectively creating a user interface for it which makes calling the program automatically problematic.

Do not get into this habit, instead get into the habit of knowing to use your OS.
Aug 15 '08 #9

weaknessforcats
Expert Mod 5K+
P: 9,197
Ok, so it runs fine in the compiler, but when I build a project and run it outside of Visual C++, it simply runs through the program until no more input is needed, then exits. Should I put in the code to stop it doing this now? Or am I doing something wrong?
When Visual Studio stops your debug program, it is a courtesy to the debugger. You never want to add code to a debug program that has to be taken out before the program can go to release. If you do, the entire program has to be re-tested (which means the debug code has to go back in) plus it means the code being tested is never the code that will be released.

When you are not in Visual Studio, your program is on its own. Then you will have to add code to pause the program when you want.
Aug 15 '08 #10

P: 5
Yeah, I run it by double clicking in installer. The problem is that when I send it to someone, it shuts off once no more input is required as well. As opposed to letting them read the output.
Aug 16 '08 #11

Banfa
Expert Mod 5K+
P: 8,916
I would say run it from a batch file and let the batch file create the pause or if you want create a gui apop that runs the program and captures the output and displays it (although that seems a little silly and over kill).

However if you are going to program in a press and key to close function you need to output a message to that effect to the console.
Aug 16 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.