Question

Here's my code so far:

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int main()
{
    int count = 0;
    string fileName;
    string keyWord;
    string word;


    cout << "Please make sure the document is in the same file as the program, thank you!" 
         << endl << "Please input document name: " ;
    getline(cin, fileName);
    cout << endl;

    cout << "Please input the word you'd like to search for: " << endl;
    cin >> keyWord;
    cout << endl;
    ifstream infile(fileName.c_str());
    while(infile.is_open())
    {
        getline(cin,word);
        if(word == keyWord)
        {
            cout << word << endl;
            count++;
        }
        if(infile.eof())
        {
            infile.close();
        }

    }
    cout << count;

}

I'm not sure how to go to the next word, currently this infinite loops...any recommendation?

Also...how do I tell it to print out the line that that word was on?

Thanks in advance!

Was it helpful?

Solution

while(infile >> word)
{
    if(word == keyWord)
    {
        cout << word << endl;
        count++;
    }
}

This would do the job. Please read about streams more.

OTHER TIPS

If all you want to do is count the number of keywords in a file then:

int count = std::count(std::istream_iterator<std::string>(infile),
                       std::istream_iterator<std::string>(),
                       keyword);

If you want to read words.
But also want to print the line numbers then somthing like this should work:

std::string      line;
std::ifstream    infile("plop");
int              lineNumber = 0;

while(std::getline(infile, line)) 
{
    ++lineNumber ;
    std::stringstream   linestream(line);
    int hits = std::count(std::istream_iterator<std::string>(linestream),
                          std::istream_iterator<std::string>(),
                          keyword);
    if (hits != 0)
    {
        std::cout << "Line: " << lineNumber << "   Matches(" << hits << ")\n";
    } 
    count  += hits;
} 

Change while(infile.is_open()) to while(infile). Then you can remove the redundant eof test at the end.

It's still open even if you've encountered an error or reached the end of file. It's likely you are in a scenario where failbit is getting set (getline returns nothing), but eof has not been encountered, so the file never gets closed, so your loop never exits. Using the operator bool of the stream gets around all these problems for you.

The problem comes in this part of the source code:

getline(cin,word);

if(word == keyWord)
{
    cout << word << endl;
    count++;
}

First of all, you don't want to read lines from cin. You want to read words from infile. So you should replace the first line of your code inside the loop by:

infile >> word;
if(word == keyWord)
    {
        cout << word << endl;
        count++;
    }

Also, you should change the condition of the loop. You don't need to check if the infile is open here. You should check for that before the loop starts. For the loop, you need to check whether the eof state has been reached or not:

if ( !infile.is_open() ) {
    cerr << "Error while opening file." << endl;
    exit( EXIT_FAILURE );
}    

while( !infile.eof() ) {
    infile >> word;
    if(word == keyWord)
    {
        cout << word << endl;
        count++;
    }
}

And as you can see, now you can get rid off that strange second if you put inside the loop.
Last step is to introduce the "reading ahead" technique: it does not make sense to test for eof when we haven't read anything.

if ( !infile.is_open() ) {
    cerr << "Error while opening file." << endl;
    exit( EXIT_FAILURE );
}    

infile >> word;    
while( !infile.eof() ) {
    if( word == keyWord )
    {
        cout << word << endl;
        count++;
    }

    infile >> word;
}

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top