50
votes

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.

4
If the error message suggests a change, why not try it and see what happens?abiessu
I did. Only more errors cropped up. More than one error.user1800967
Which compiler? What OS? What are some examples of the new errors that showed up? What are your build settings and/or what is your compile command?abiessu
Well, this is on windows though it was originally meant to be run on a Linux machine. Might that be it?user1800967
if a compiler warns about a header you never heard of - google the header and find out what is supposed to be in itGlenn Teitelbaum

4 Answers

109
votes

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".

27
votes

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

#include <stdafx.h>

Visit here to understand Precompiled Headers

5
votes

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

-2
votes

Put this at every source file of your project at the top

#include <stdafx.h>

or / and

Your .cpp file is probably not in the same directory as pch.h