Hi, im still new in c++ so i'm happy if anyone can help me

Hi, is anyone know how to search two text file and the output will be

  • show which text file
  • line number
  • the word itself

currently what i did is i need the user to key in the text file. i dont want to do that, i want it to be

search word : small

and it will show the
filename
line no
small (which the word i search)

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

using namespace std;

int searchWord()
{
   string input_file,searchWord,line;
   while (1)
    {
        int line_Number=0,found=0;
        cout<<"File: ";
        getline(cin,input_file);
        if (input_file == "exit")
        break;

        cout<<"Search Word: ";
        getline(cin,searchWord);
        ifstream file(input_file.c_str());
        if(file)
        {
            while(getline(file,line))
            {
                line_Number++;
                int position=0;
                for(int i=line.find(searchWord); i<line.length(); i=i+position)
                {
                    position=line.find(searchWord,i);
                    if(position != string::npos)
                    {
                        cout<<endl<<searchWord<<" is at line "<<line_Number<<endl;
                        found=1;
                    }
                    else break;
                }
            }
            file.close();
            if(found==0)
            {
                cout<<endl<<searchWord<<" not in file"<<endl;
            }
        }
        else
        {
            cout<<endl<<input_file<<" not found" <<endl;
        }

}
}
int main()
{

    searchWord();

    return 0;

}

my text file is below

file1

The study is published in the journal Nature Climate Change.
It focuses on the Scotia Sea and the Antarctic Peninsula - the places
where the crustaceans are most abundant.

file2

The team's analysis indicates the centre of krill distribution has now
moved to where more favourable conditions are found, tracking
southward towards the Antarctic continent by about 440km, or four
degrees of latitude.

But word small not in your text file examples

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.