Error searching

closed account (iN8poG1T)
Hi, i want to make a program that can search multiple text file. I am still learning. If i enter any words, it will output which text file, and also line, position of the word i enter. Below is i need to enter the the specific file, what i want to do is, just search the word and will output which text file and line.
also, when i try to run my code below, when i specific my filename, i get infinite loop but some is not. why is that happen?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    string input_file,searchWord,line;
    while (1)
    {
        int line_Number=0,found=0;
        cout<<"File: ";
        getline(cin,input_file);
        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_Number<<":"<<position<<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;
        }

    return 0;
    }
 }


below is my text file

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.


lets say, i want to search for the word "study"
it will output the file1 and line 1, position 2
You need to find an exit condition for the outer while loop.
Maybe use exit or sth. like this for the file name.
1
2
3
4
5
6
7
8
9
while (1)
{
  int line_Number=0,found=0;
  cout<<"File: ";
  getline(cin,input_file);
  if (input_file == "exit")
    break;
  // search the file here
}

Also it might be a good idea to put the code for searching into a separate function.
closed account (iN8poG1T)
Hai I change my code and make one function for search. but i just want to search the word without asking which filename. because later when i search a word, it will input, the filename, line, and also the word itself. how can i do that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#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_Number<<":"<<position<<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;

}
Topic archived. No new replies allowed.