Searching within 2 Files

Hey Guys,

I did this coding. This function will search through 2 files.

But when even I enter any number to search it goes to the previous menu.

Mainly I wanted this function to do is that if i search for a number, It should first search the first file, if found it will display the result and if not found it will search the second file.

and display the result.

Please check the code tell me where am i going wrong?

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
void R_search()
{
fstream file1;
fstream file2;
char *temp;
cout<<"Enter Phone";
gets(temp);
file1.open("person.txt",ios::app|ios::out);
if(!file1)
            {
            cout<<"\nError in opening the file PERSON ";
            getch();
            }
            else
            {

            file1.read((char *)&person,sizeof(person));
            while(!file1.eof())
            {
                 int i=strcmp(person.retphone_person(),temp);
                    if(i==0)
                    {
                    person.display_person();
                    getch();
                    }

            }
}
file2.open("busniess.txt",ios::app|ios::out);
if(!file2)
            {
            cout<<"\nError in opening the file BUSNIESS";
            getch();
            }
            else
            {
            file2.read((char *)&busniess,sizeof(busniess));
            while(!file2.eof())
            {
                    int i=strcmp(busniess.retphone_busniess(),temp);
                    if(i==0)
                    {
                    busniess.display_busniess();
                    getch();
                    }

            }
}
file1.close();
file2.close();
}
If you write to stdout with std::cout, you should read from stdin using std::cin as the streams are "tied". That is, they're sync'd.

You've used gets(temp), but not inialised temp. gets is writing wherever temp happens to be pointing to.

Why not use std::cin to read the input into a string instead of messing around with pointers that you're uncomfortable with?
Topic archived. No new replies allowed.