Cant Search 2 files together :(

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 search 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?

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();
}

Replies

  • shalini_goel14
    shalini_goel14
    Hey anshu ! Can you show me the output, you are getting with your code right now? If I am not wrong, your shared code is not complete right?

    Where are C++ users ? ๐Ÿ˜
  • Prasad Ajinkya
    Prasad Ajinkya
    Anshu,
    I havent done C/C++ coding in more or less a decade now ๐Ÿ˜€, but here are some things I would want you to check -
    - person.retphone_person() return type is a string and not an integer
    - we might need to see your person class as well
  • anshuverma1989
    anshuverma1989
    Hey Guys..

    Thanks for your reply..

    here is my whole code

    #include
    #include
    #include
    #include
    #include
    #include
    
    class PERSON
    {
    
        char name[20];
        char ***;
        char phone[20];
        char add[100];
        char email[30];
        public:
        void add_person();
        void delete_person();
        void display_person();
        void modify_person();
        void search_person();
        void menu_person();
            char *retname_person()
        {return name;}
        char *retphone_person()
        {return phone;}
    }person;
    
    
    
                 void PERSON::add_person()
                 {
                      clrscr();
                      cout<<"\n Person\n";
                      cout<<"\nEnter the Name:  ";
                      gets(name);
                        do
                        {    cout<<"\nEnter the ***  :  ";
                            cin>>***;
                            ***=toupper(***);
                        }while(***!='M' && ***!='F');
                        cout<<"\nPhone Number: ";
                        gets(phone);
                        cout<>choice;
              switch(choice)
              {
              case 1:
                clrscr();
               fstream file;
               file.open("person.txt",ios::app|ios::out);
                   person.add_person();
                   file.write((char *)&person,sizeof(person));
                   file.close();
               break;
              case 2:
                clrscr();
               person.delete_person();
               break;
              case 3:
                clrscr();
               person.modify_person();
               break;
              case 4:
                clrscr();
               person.search_person();
               break;
          case 5:
          clrscr();
          choice = 0;
          break;
              default :
              cout<<"Invalid";
              clrscr();
          }
              }while(choice);
    
             }
    
    
    class BUSNIESS
    {
    
              char name[20];
              char phone[20];
              char add[100];
              char ***;
              char company_name[50];
              char email[30];
              char website[30];
          public:
        void add_busniess();
        void delete_busniess();
        void display_busniess();
        void modify_busniess();
        void menu_busniess();
        void search_busniess();
        char *retname_busniess()
        {
                return name;
        }
                  char *retphone_busniess()
        {
                return phone;
        }
    
    }busniess;
    
    void BUSNIESS::add_busniess()
             {
                      clrscr();
                      cout<<"\n Busniess\n";
                      cout<<"\nEnter the Name:  ";
                  gets(name);
                        do
                {    cout<<"\nEnter the ***  :  ";
                            cin>>***;
                            ***=toupper(***);
                        }while(***!='M' && ***!='F');
                        cout<<"\nPhone Number: ";
                        gets(phone);
                        cout<>choice;
              switch(choice)
              {
              case 1:
               clrscr();
               fstream file;
               file.open("busniess.txt",ios::app|ios::out);
               add_busniess();
                       file.write((char *)&busniess,sizeof(busniess));
                       file.close();
               break;
              case 2:
               clrscr();
               delete_busniess();
               break;
              case 3:
                 clrscr();
               modify_busniess();
               break;
              case 4:
               clrscr();
               search_busniess();
               break;
              case 5:
                clrscr();
               choice = 0;
               break;
              }
              }while(choice);
             }
    
    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();
    }
    
    
    
    
    int main()
    {
    clrscr();
    int choice = 1;
    do
    {
    cout<<"\n*******************************\n411 Directory\n\n********************************\n\n"<>choice;
    switch(choice)
    {
    case 1:
        clrscr();
          person.menu_person();
          break;
    case 2:
        clrscr();
          busniess.menu_busniess();
          break;
    case 3:
        clrscr();
          R_search();
          break;
    case 4:
         choice = 0;
         break;
    default :
         cout<<"INVALID INPUT";
         clrscr();
    }
    }while(choice != 0);
    return 0;
    }
    
  • shalini_goel14
    shalini_goel14
    *bump*

    @Slashfear This guy need your help ๐Ÿ˜€
  • slashfear
    slashfear
    Hey anush,

    wuff.... ok I will take it from here Shalini ;-)....... Let me see what i can do......๐Ÿ˜’

    Hmmm ok so here is what I can help buddy, First of all sorry buddy, i couldn't go through your code fully(as i am little busy๐Ÿ˜”) any way's I will show you an sample of how to do the searching, just replace the appropriate function and file name to be searched in the below code and include this piece of code in your R_search():

    So here is a tidbit of how you would do the comparison of the input with the person's info stored in a file...
    if (strcmp(telephonevar, book[i]->getTelephone()) == 0) {
         // found telephone number owner
         // Print the name and address
         cout << "Name: " << book[i]->getName() << " address: " << book[i]->getAddress() << endl;
    }
    
    What you will need is an array of these person classes which will serve as your "telephone" book and a function that will run a search on this array. So lets say that someone types in a phone number, you then loop through the array of Person classes comparing the input with the persons telephone number using the class' getTelephone() accessor method. When you find a match, then you just access the class and display the name and address.

    If you put that into a loop where "book" is an array of Person classes and "i" is the loop's index variable, you can quickly locate the person with the phone number and similarly for searching the second file you can use an else statement after the if and give the info of the second file to be searched....๐Ÿ˜

    Try this and you will get the desired output buddy, and if you still have any prob just let us know....!!๐Ÿ˜‰


    -Arvind(slashfear)

You are reading an archived discussion.

Related Posts

Hi Guys, Please feel free to download our C4 calculator which includes a voltage gap calculator and a current carrying capacity of tracks calculator. Do you need to know how...
Weโ€™ll see a bit about the basic strucutre of a robot today. There are certain basic componentrs or modules, which together make a robot. There are many of them. Some...
Google Image Search joins Google's web search and Google Video in offering suggestions to auto-complete your search, often with a "wallpaper," "logo," or other popular terms. Don't like the clutter?......
Hi all, I recently read about this in a newspaper. Recently, in Britain, there have been voices suggesting the top business schools played a part in the current economic crisis....
Hi all, I came across this picture in the internet. I give this to you all to connect. I got the connect quite easily. Well, I will tell you now...