file search even displaying part of word

closed account (1vf9z8AR)
The search works but when i type a letter or continous letters of a word it displays word found even then.

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
59
60
61
62
63
64
65
66
67
68
69
70
  #include<iostream>
#include<stdlib.h>
#include<fstream>
#include<string>
using namespace std;
char line[80];
void write()
{
    char ch;
    ofstream w1;
    w1.open("text.txt");
    do
    {
        cout<<"Enter line(dot to terminate)"<<endl;
        cin.getline(line,80,'.');
        w1<<line;
        cout<<"More lines?(y/n)";cin>>ch;cout<<endl;
    }while(ch=='y');
    w1.close();
}
void read()
{
    ifstream r1;
    r1.open("text.txt");
    while(!r1.eof())
    {
        r1>>line;
        cout<<line<<endl;
    }
    r1.close();
}
void searchf()
{
    int a;
    string s;
    string l;
    ifstream f1;
    cout<<"Enter line you want to search";cin>>l;
    f1.open("text.txt");
   while(!f1.eof())
   {
     getline(f1,s);
     if((((a=s.find(l,0)))!=string::npos))
        cout<<"Word found="<<l<<endl;
   }
}
int main()
{

    int n;
    char ch;
    do
    {
    cout<<"1-write file"<<endl;
    cout<<"2-read file"<<endl;
    cout<<"3-search file"<<endl;
    cout<<"Enter option:";cin>>n;cout<<endl;
    if(n==1)
        write();
    else if(n==2)
        read();
            else if(n==3)
                searchf();
    else
        cout<<"Enter correct option!!"<<endl;
    cout<<"Do you want to continue(y/n)?";cin>>ch;
    cout<<endl;
    }while(ch=='y');
}

can tell me a better search program for text files?
I’m not going to try to discuss your code in my poor English, but I hope the following one could give you some hints. The part where the user is asked for input should be rewritten, but I tried to stuck to your code as much as I could.
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <fstream>
#include <iostream>
#include <limits>
#include <string>

void write(); // write() already exists in std namespace
std::string read(); // read() already exists in std namespace
void searchf();
void waitForEnter();

int main()
{
    char ch;
    do {
        std::cout << "1 - write file"
                  << "\n2 - read file"
                  << "\n3 - search file"
                  << "\n\nEnter option: ";
        int n;
        std::cin >> n;
        std::cin.ignore(1);
        switch(n) {
        case 1:
            write();
            break;
        case 2:
            read();
            break;
        case 3:
            searchf();
            break;
        default:
            std::cout << "Enter an option between the above ones!!\n"
                      << "Do you want to continue(y/n)? ";
            std::cin >> ch;
            std::cin.ignore(1);
            break;
        }
    } while(ch == 'y');
    waitForEnter();
    return 0;
}

void write()
{
    std::ofstream w1("text.txt");
    char ch {};
    do {
        std::cout << "Enter line(dot to terminate): ";
        std::string line;
        std::getline(std::cin, line, '.');
        w1 << line;
        std::cout << "More lines (y/n)? ";
        std::cin >> ch;
        std::cin.ignore(1);
    } while(ch == 'y');
    w1.close();
}

std::string read()
{
    std::ifstream r1("text.txt");
    std::string line;
    while(std::getline(r1, line))
    {
        std::cout << line << '\n';
    }
    r1.close();
    return line;
}

void searchf()
{
    std::cout << "Enter the word line you want to search for: ";
    std::string l;
    std::cin >> l;
    std::cin.ignore(1);
    std::ifstream f1("text.txt");
    std::string s;
    while(f1 >> s) // white spaces are ignored, but punctuation isn't
    {
        if(s == l) {
            std::cout << "Word " << l << " found.\n";
            return;
        }
    }
    std::cout << "Can't find " << l << '\n';
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

closed account (1vf9z8AR)
thanks.
Topic archived. No new replies allowed.