Cant Search 2 files together :(

anshuverma1989

anshuverma1989

@anshuverma1989-8zd8dA β€’ Oct 26, 2024
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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • shalini_goel14

    shalini_goel14

    @shalini-goel14-ASmC2J Apr 7, 2009

    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

    @prasad-aSUfhP Apr 7, 2009

    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

    @anshuverma1989-8zd8dA Apr 7, 2009

    Hey Guys..

    Thanks for your reply..

    here is my whole code

    #include<fstream.h>
    #include<ctype.h>
    #include<conio.h>
    #include<stdio.h>
    #include<string.h>
    #include<process.h>
    
    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<<endl;
                        cout<<"\nAddress: ";
                        gets(add);
                        cout<<endl;
                        cout<<"\nEmail: ";
                        gets(email);
                        cout<<endl;
                           cout<<"\nThank You for Entering Records:";
                         getch();   
                 }
    
    
                 void PERSON::delete_person()
                 {
                          fstream file;
                        fstream ofs;
                        clrscr();
                char temp[20];
                        file.open("person.txt",ios::in);
                ofs.open("temp.txt",ios::out);
                if(!file)
                {
                cout<<"\nError in opening the file ";
                getch();
                }
                else
                {
                cout<<"Enter Name to be deleted";
                gets(temp);
                file.read((char *)&person,sizeof(person));
                while(!file.eof())
                {
                     
                     int i=strcmp(retname_person(),temp);
                        if(i!=0)
                        {
                                ofs.write((char*)&person,sizeof(person));
                        }
                        else
                        {
                        cout<<"Deleted Profile";
                        display_person();
                        }
                file.read((char *)&person,sizeof(person));
                }
                file.close();
                ofs.close();
                remove("person.txt");
                rename("temp.txt","person.txt");
             }
             }
             void PERSON::display_person()
             {
              cout<<"Name ";
              puts(name);
                      cout<<"\nSex ";
                      cout<<***;  
              cout<<"\nPhone Number ";
              cout<<phone;
              cout<<"\nAddress ";
                      puts(add);
              cout<<"\nEmail ";
                      puts(email);
          }
    
             void PERSON::modify_person()
             {
              fstream file;
                fstream ofs;
                clrscr();
                int g1=0;
                char temp[20];
                file.open("person.txt",ios::in);
                ofs.open("temp.txt",ios::out);
                if(!file)
                {
                cout<<"\nError in opening the file ";
                getch();
                }
                else
                {
                cout<<"Enter Name to be modify";
                gets(temp);
                file.read((char *)&person,sizeof(person));
                while(!file.eof())
                {
    
                     int i=strcmp(retname_person(),temp);
                        if(i!=0)
                        {
                                ofs.write((char*)&person,sizeof(person));
                        }
                        else
                        {
                                    cout<<"Record Found.Press any key to edit.";
                                    g1=1;
                                    getch();
                        add_person();
                        ofs.write((char*)&person,sizeof(person));
                        }
                                    file.read((char *)&person,sizeof(person));
                }
    
    
             }
             if(g1==0)
                {
                cout<<"\nData not found";
                getch();
                }
             file.close();
             ofs.close();
             remove("person.txt");
             rename("temp.txt","person.txt");
             }
    
    
             void PERSON::search_person()
             {
                fstream file;
                clrscr();
                int g=0;
                char temp[20];
                file.open("person.txt",ios::in);
                if(!file)
                {
                cout<<"\nError in opening the file ";
                getch();
                }
                else
                {
                cout<<"Enter Name to be search";
                gets(temp);
                file.read((char *)&person,sizeof(person));
                while(!file.eof())
                {
                 if(!strcmp(retname_person(),temp))
                        {
                                cout<<"Search Result\n";
                                display_person();
                                g=1;
                                getch();
                        }
                file.read((char *)&person,sizeof(person));
                }
                if(g==0)
                {
                cout<<"\nData not found";
                getch();
                }
              file.close();
                }
                 }
    
    
              void PERSON::menu_person()
             {
              clrscr();
              int choice = 1;
              do
              {
              clrscr();
              cout<<"1. Add Details\n2. Delete Details\n3. Modify Details\n4. Search Details\n5. Back to Main Menu\n";
              cin>>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<<endl;
                        cout<<"\nCompany Name: ";
                        gets(company_name);
                        cout<<endl;
                            cout<<"\nAddress: ";
                        gets(add);
                        cout<<endl;
                            cout<<"\nEmail: ";
                gets(email);
                        cout<<endl;
                        cout<<"\nWebsite: ";
                        gets(website);
                cout<<endl;
                        cout<<"\nThank You for Entering Records:";
                getch();
                      
             }
    
                 void BUSNIESS::delete_busniess()
                 {
                        fstream file;
                    fstream ofs;
                        clrscr();
                char temp[20];
                        file.open("busniess.txt",ios::in);
                        ofs.open("temp.txt",ios::out);
                        if(!file)
                        {
                cout<<"\nError in opening the file ";
                        getch();
                        }
                        else
                {
                        cout<<"Enter Name to be deleted";
                gets(temp);
                        file.read((char *)&busniess,sizeof(busniess));
                        while(!file.eof())
                        {
                           
                                 int i=strcmp(retname_busniess(),temp);
                                     if(i!=0)
                                      {
                                            ofs.write((char*)&busniess,sizeof(busniess));
                                      }
                                    else
                                    {
                                    display_busniess();
                                    }
                                     file.read((char *)&busniess,sizeof(busniess));
                        }
                 file.close();
                 ofs.close();
             remove("busniess.txt");
                 rename("temp.txt","busniess.txt");
    
                 }
                 }
                 void BUSNIESS::display_busniess()
                 {
                      cout<<"\nName ";
                      puts(name);
                      cout<<"\nSex ";
                      cout<<***;  
              cout<<"\nPhone Number ";
                      cout<<phone;
                      cout<<"\nCompany ";
                      puts(company_name);
                      cout<<"\nAddress ";
              puts(add);
                      cout<<"\nEmail ";
                      puts(email);
                      cout<<"\nWebSite ";
                      puts(website);
                      
                 }
                 void BUSNIESS::modify_busniess()
                 {
                        fstream file;
                fstream ofs;
                clrscr();
                int g2=0;
                char temp[20];
                file.open("busniess.txt",ios::in);
                ofs.open("temp.txt",ios::out);
                if(!file)
                {
                cout<<"\nError in opening the file ";
                getch();
                }
                else
                {
                cout<<"Enter Name to be modify";
                gets(temp);
                file.read((char *)&busniess,sizeof(busniess));
                while(!file.eof())
                {
    
                     int i=strcmp(retname_busniess(),temp);
                        if(i!=0)
                        {
                                ofs.write((char*)&busniess,sizeof(busniess));
                        }
                        else
                        {
                        g2=1;
                        add_busniess();
                        ofs.write((char*)&busniess,sizeof(busniess));
                        }
                                    file.read((char *)&busniess,sizeof(busniess));
    
                }
    
    
             }
             if(g2==0)
                {
                cout<<"\nData not found";
                getch();
                }
             file.close();
             ofs.close();
             remove("busniess.txt");
                 rename("temp.txt","busniess.txt");
             }
    void BUSNIESS::search_busniess()
             {
                fstream file;
                int g=0;
                clrscr();
                char temp[20];
                file.open("busniess.txt",ios::in);
                if(!file)
                {
                cout<<"\nError in opening the file ";
                getch();
                }
                else
                {
                cout<<"Enter Name to be search";
                gets(temp);
                file.read((char *)&busniess,sizeof(busniess));
                while(!file.eof())
                {
    
                     int i=strcmp(retname_busniess(),temp);
                        if(i==0)
                        {
                                cout<<"\nSearch Details\n";
                                display_busniess();
                                getch();
                                g=1;
                        }
                        file.read((char *)&busniess,sizeof(busniess));
                }
             }
             file.close();
             if(g==0)
             {
            cout<<"\nValue Data for this name\n";
            getch();
             }
             }
             void BUSNIESS::menu_busniess()
             {
              clrscr();
              clrscr();
              int choice = 1;
              do
              {
              clrscr();
              cout<<"1. Add Details\n2. Delete Details\n3. Modify Details\n4. Search Details\n5. Back to Main Menu";
              cin>>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"<<endl;
    cout<<"1. Person \n2. Business \n3. Reverse Telephone Number \n4. Exit";
    cin>>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

    @shalini-goel14-ASmC2J May 8, 2009

    *bump*

    @Slashfear This guy need your help 😀
  • slashfear

    slashfear

    @slashfear-tSWzpz May 8, 2009

    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)