473,394 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

#include <iostream>, Precompiled headers error

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
11 41237
oler1s
671 Expert 512MB
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
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
9,065 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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
9,208 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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
9,208 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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

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

Similar topics

10
by: John Tiger | last post by:
Can anybody have idea about the difference between #include <iostream.h> and #include <iostream>. Is later one valid statement on any compiler. I tried compiling on MSVC second statement give...
9
by: ai | last post by:
Hi , I have an application which has to read files of size few mba nd read them into array . Since i am using STL and std namespace to avoid the error error C2872: 'ifstream' : ambiguous...
4
by: Exits Funnel | last post by:
Hello, I'm slightly confused about when to use parens around #included files and when to use angle brackets. I understand (I think) that the difference is that the compiler will search in its...
1
by: zerotyNONONOTHERESNOSPAMMING!type | last post by:
Hi, As most of you probably already know, depending on which implementation of C++ you have, #include <iostream> may cause various other headers to be #included too. (I've observed cstring,...
1
by: ya man | last post by:
when i use #include <iostream.h> in some files i get lots of error messages of the kind 'ambiguous symbol this is solved when i use #include <iostream why is that ? and can i use #include...
4
by: nils | last post by:
Hi all, Recently I started migrating a gcc project to Visual Studio C++ (dotnet). The problem is that I cannot include any iostream header-file: #include <fstream> int main (int argc, char *...
2
by: Brett | last post by:
Hi, I'm compiling an old project under the 'new' visual studio 7.1.3088. I changed the line: #include <iostream.h> to #include <iostream> and now I get a stack of errors, some of which...
9
by: nichas | last post by:
I tried to use #include<iostream> in visual C++ compiler but then it didnt work but i see this is the way it is being mentioned in C++ primer by lippman and lajoie.. where is the problem.. Do...
6
by: Bernd Gaertner | last post by:
Dear experts, according to the standard, manipulators like noskipws are declared in the header ios (). But when I look at code that is around, I usually see #include<iomanipbeing used. What is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.