56

I compile the following code but I get a compile error in Visual Studio that I cannot understand.

#include <iostream>

using namespace std;

int main()
{
    int matchCount, findResult;
    long childPID;
    string userInput = "blank";

    // string to be searched through
    string longString = "The PPSh-41 is a Soviet submachine gun designed by Georgi Shpagin as an inexpensive, simplified alternative to the PPD-40.";

    while (userInput.compare("!wq"));
    {
        // reset variables for reuse
        matchCount = 0;
        findResult = -1;

        cout << "Please enter a word/s to search for (!wq to exit): "; // prompts user for string to search for
        cin >> userInput; // takes user input

        if (userInput.compare("!wq")) // checks user input to see if they still wish to search for a string
        {
            childPID = fork();

            if (childPID == 0)
            {
                while (findResult < longString.length)
                {
                    findResult = longString.find(userInput, findResult + 1, userInput.length);

                    if (findResult < longString.length)
                        matchCount++;
                }

                cout << "There are " << matchCount << " instances of " << userInput << " in longString." << endl;
            }
            else
                cout << "childPID != 0" << endl;
        }
        else
            cout << "User has chosen to exit. Exiting." << endl;
    }

    return 0;
}

The error reads:

"wordcount.cpp(57) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?"

I don't believe I need a header file to run this code. Thank you for all your help in advance.

Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80
user1800967
  • 953
  • 1
  • 6
  • 11

3 Answers3

127

Look at https://stackoverflow.com/a/4726838/2963099

Turn off pre compiled headers:

Project Properties -> C++ -> Precompiled Headers

set Precompiled Header to "Not Using Precompiled Header".

Community
  • 1
  • 1
Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80
  • 3
    @user1800967 - in VS2010 - `Common Properties`->`Configuration Properties`->`C/C++`->`Precompiled Headers` ; then `Precompiled Header` should be `Not Using Precomiled Header` – Glenn Teitelbaum Nov 21 '13 at 05:59
  • 3
    It's worth noting that if you still get the same error, it might be because you didn't turn off the option in the correct release configuration or platform. – Nicolás de Ory Aug 25 '20 at 10:53
  • You can turn it off for a selection of source files. Which is useful for side modules not related to you AFX project. (Click right on each file) Practice: It's better to group the not AFX files. – Sandburg Jun 02 '22 at 07:31
28

The first line of every source file of your project must be the following:

#include <stdafx.h>

Visit here to understand Precompiled Headers

asif
  • 975
  • 8
  • 16
  • 6
    Not true, the answer is to turn off precompiled headers. – john Nov 21 '13 at 05:49
  • 3
    @john either way works. Precompiled headers can (but don't always) significantly improve compile speed, though they do have some disadvantages, too. If user1800967 wants to use precompiled headers, he can add #include as the first non-comment line in his source files. asif is incorrect about it being required to be the first line in the source file, but it does need to be the first non-comment line in the source file. – George Oct 19 '15 at 12:09
  • 7
    Looks wrong. It's `#include "stdafx.h"`, not `#include `. – user202729 Jun 28 '18 at 01:46
  • 5
    This should be the accepted answer. Solutions that say "just turn off the feature if it's not working for you" are not really solutions at all. – carthurs Oct 02 '18 at 11:08
5

Create a new "Empty Project" , Add your Cpp file to the new project, delete the line that includes stdafx.

Done.

The project no longer needs the stdafx. It is added automatically when you create projects with installed templates. enter image description here

Zahid Rouf
  • 1,611
  • 2
  • 12
  • 10
  • This works when using Visual Studio 2017 Community Edition. Compiles on the first attempt in 64-bit mode with a few warnings about numeric conversions that may lead to data loss. – Ray Goudie Jan 21 '18 at 20:08