searching in a file

I'm trying to make a simple program which searches for a certain record in file but some why it the search member function doesn't produce any output can any body help??????????
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
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

class person
{
private:
    char FirstName[20];
    char LastName[20];
    char address[20];
    void readPerson()
    {
        cout<<"Enter first Name: ";
        cin>>FirstName;
        cout<<"Enter last name";
        cin>>LastName;
        cout<<"Enter address: ";
        cin>>address;
    }
public:
    void putPerson(char fileName[])
    {
        ofstream file;
        file.open(fileName, ios::out);
        char ch;
        do
        {
            readPerson();
            file<<FirstName<<"|";
            file<<LastName<<"|";
            file<<address<<"|"<<"\n";
            cout<<"Enter new record (y/n)?";
            cin>>ch;
        }while (ch == 'y');
        file.close();
    }
    void search(char fileName[],char firstName[])
    {
        ifstream file;
        file.open(fileName);
        int found=0;
        while(!file.eof())
        {
            file.getline(FirstName, 20, '|');
            file.getline(LastName, 20, '|');
            file.getline(address, 20, '|');
            if(strcmp(firstName, FirstName)==0)
            { found=1;

               cout<<FirstName<<LastName<<address;
            }

        }
        if(found==0)
            cout<<"record not found"<<endl;
    }

};

int main()
{
    person p;
    char fn[20];
    char fileName[20];
    cout<<"Enter file name";
    cin>>fileName;
    p.putPerson(fileName);
    cout<<"Enter first name: ";
    cin>>fn;
    p.search(fileName, fn);
}
Last edited on
the search member function doesn't produce any output
It does. You can't see it because the console (in your debug environment) closes to quickly. Call your programm from the console directly or use a better IDE like Code::Blocks
first of all thx 4 u concern

I tried to run it from the console but the same problem appeared
it found the record only if it was the 1st record other wise it returns record not found :(
Yes I see. The problem is on line 33. There you add '\n'. But when reading you're just looking for '|' (and not '\n') hence it never reaches the end of file and you have an endless loop.
Topic archived. No new replies allowed.