0

I just started C++ I am reading the ebook Starting out with C++ 7th edition. I copied the code from the book and put it into Visual, under new project w32 console app with precompiled headers. Well When I use the iostream in the preprocessor directive line i get.. I searched around and do not understand why the iostream won't work, any help?

1>------ Build started: Project: dd, Configuration: Debug Win32 ------
1>  stdafx.cpp 1>  dd.cpp 1>c:\documents and settings\leon\my documents\visual studio 2010\projects\dd\dd\dd.cpp(24): 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 ==========
    1 // This program calculates the user's pay.
    2 #include <iostream>
    3 using namespace std;
    4
    5 int main()
    6 {
    7 double hours, rate, pay;
    8
    9 // Get the number of hours worked.
    10 cout << "How many hours did you work? ";
    11 cin >> hours;
    12
    13 // Get the hourly pay rate.
    14 cout << "How much do you get paid per hour? ";
    15 cin >> rate;
    16
    17 // Calculate the pay.
    18 pay = hours * rate;
    19
    20 // Display the pay.
    21 cout << "You have earned $" << pay << endl;
    22 return 0;
    23 }

5 Answers 5

2

It's not because of iostream, but because you forgot to include stdafx.h.

The error is pretty clear on that. If you build projects with precompile headers, you have to include stdafx.h at the beginning of your implementation files.

0
2

In Visual C++, project can use what is called "pre-compiled headers". This is a technique to help speed up building of the project. For this to work you need to #include the file "stdafx.h" as the first thing you do in the source file.

So add this line before the inclusion of iostream:

#include "stdafx.h"

When you build again it should work.

5
  • I just did that and now I am getting missing function header. Feb 15, 2012 at 10:25
  • @user1211008 Did you add the new line before you include iostream? It must always be the first non-comment line in the source files. You might also try to rebuild the solution. Feb 15, 2012 at 10:30
  • I found it. Misplaced ; after int main() Feb 15, 2012 at 10:31
  • Got it working, for a bit, I just wanted to see if I could even start on a textbook example. Thank you for the help. Now to solve this issue,, googling..Cannot find or open the PDB file Feb 15, 2012 at 10:34
  • okay for some reason the pdb files were not checked in the debug options... just checked them and hit debug for some reason C++ is trying to load avast snxhk.dll and cannot find it Feb 15, 2012 at 10:40
1

Include the following header ..

#include "stdafx.h"
0
0

Try this:

#include "stdafx.h"
#include "iostream"    

(remove .h extension - VS2010 seems to not find the header file with that file extension)?

-1

When you create the new project, instead of selecting "w32 console app", select "Empty Project", then create a new CPP file, and the code should work!

2
  • I did that and now it cant find the specified file hours.exe, after I debug the source code. Feb 15, 2012 at 10:52
  • I understand why it is saying that but I thought when you, debug it creates a .exe? Feb 15, 2012 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.