Thread: Binary files comparing problems

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    16

    Binary files comparing problems

    i am working on a airport reservation program and i have run into a brick wall. i want to ask the user its name,gender,passport no,age, destination, and travel class and figure out the day and flight code of the flight which i have saved in a binary file. now every thing works fine except the code and the day. could you help me figure out the problem.
    the programs important section

    the flight class
    Code:
    class flights
        {
        char code[9],location[21];
        public:
        void display();
        char *retloc()                                         //to get the Location
        {return location;}
        char *retcode()                                     //to get the flight code
        {return code;}
      };
    the ticket class
    Code:
    class ticket                                                      //Class ticket  {
      private:
      char name[50];                                              //Passenger's name
      char p_num[25];                                              //Passport number
      char des[50];                                                    //Destination
      char sex;                                                //Gender of passenger
      char day[9];
      char code[9];
      int age;                //Age of passenger required for finding cost of ticket
      int t_class;                                                    //Travel class
      float price;                                                 //Price of ticket
    
    
      public:
      void enter();
      float expence(int t_c,int old);
      void display();
    
    
      char *get_num()                                   //To get passport number for
        {return p_num;}                                          // enquire function
    
    
      float get_price()                          //To get price of indivisual ticket
        {return price;}                                // for calculating total cost
    
    
      };                                                       //End of class ticket
    the enter function
    Code:
    void ticket::enter()                                                    //Enter function
      {
    
    
      cout<<"Personal Information\n\n";
    
    
      cout<<"Enter passenger's name: ";
      gets(name);
      cout<<"\nEnter passport number: ";
      gets(p_num);
      cout<<"\nEnter passenger's age: ";
      cin>>age;
      cout<<"\nEnter passenger's gender(M/F): ";
      cin>>sex;
    
    
      clrscr();
    
    
      cout<<"Flight Information"<<endl<<endl;
    
    
      cout<<"Enter destination: ";
      gets(des);
      cout<<"\nSelect type of travel class: "<<endl;
      cout<<"1)First Class"<<endl;
      cout<<"2)Business Class"<<endl;
      cout<<"3)Economy Class"<<endl;
      cout<<endl;
      cout<<"Choice: ";
      cin>>t_class;
    
    
      price=expence(t_class,age);
    
    
      int len=strlen(des);
      for(int j=0;j<20-len;j++)
        strcat(des," ");
      fstream f1;
      flights f;
      f1.open("Flight_Record.dat",ios::in|ios::binary);
      while(f1.read((char*)&f,sizeof(f)))
         if(!strcmp(des,f.retloc()))
             strcpy(code,f.retcode());
      f1.close();
      fstream f2;
    
    
      for(int i=0;i<7;i++)
       {
       if(i==0)
         {
         f2.open("Flight_RecordSunday.dat",ios::in|ios::binary);
         while(f2.read((char*)&f,sizeof(f)))
         if(!strcmp(code,f.retcode()))
           {
           strcpy(day,"SUNDAY");
           f2.close();
           return;
           }
         }
       if(i==1)
         {
         f2.open("Flight_RecordMonday.dat",ios::in|ios::binary);
         while(f2.read((char*)&f,sizeof(f)))
         if(!strcmp(code,f.retcode()))
    {
           strcpy(day,"MONDAY");
           f2.close();
           return;
           }
         }
       if(i==2)
         {
         f2.open("Flight_RecordTuesday.dat",ios::in|ios::binary);
         while(f2.read((char*)&f,sizeof(f)))
              if(!strcmp(code,f.retcode()))
    {       strcpy(day,"TUESDAY");
           f2.close();
           return;
           }
         }
       if(i==3)
         {
         f2.open("Flight_RecordWednesday.dat",ios::in|ios::binary);
         while(f2.read((char*)&f,sizeof(f)))
         if(!strcmp(code,f.retcode()))
    {       strcpy(day,"WEDNESDAY");
           f2.close();
           return;
           }
         }
       if(i==4)
         {
         f2.open("Flight_RecordThursday.dat",ios::in|ios::binary);
         while(f2.read((char*)&f,sizeof(f)))
         if(!strcmp(code,f.retcode()))
    {       strcpy(day,"THURSDAY");
           f2.close();
           return;
           }
         }
       if(i==5)
         {
         f2.open("Flight_RecordFriday.dat",ios::in|ios::binary);
         while(f2.read((char*)&f,sizeof(f)))
         if(!strcmp(code,f.retcode()))
    {       strcpy(day,"FRIDAY");
           f2.close();
           return;
           }
         }
       if(i==6)
         {
         f2.open("Flight_RecordSaturday.dat",ios::in|ios::binary);
         while(f2.read((char*)&f,sizeof(f)))
         if(!strcmp(code,f.retcode()))
    {       strcpy(day,"SATURDAY");
           f2.close();
           return;
           }
         }
    
    
       f2.close();
       }
    
    
      }
    the display function
    Code:
    
    void ticket::display()                                        //Display function
      {
      clrscr();
      cout<<"Name: ";
      puts(name);
      cout<<"\nPassport Number: ";
      puts(p_num);
      cout<<"\nAge: "<<age<<endl;
      cout<<"Sex: "<<sex<<endl;
      cout<<"From: Kuwait"<<endl;
      cout<<"To: ";
      puts(des);
      cout<<"\nDay: ";
      puts(day);
      cout<<"\nCode: "<<code;
    
    
      cout<<"\nTravel Class: ";
      switch(t_class)
       {
         case 1:cout<<"First Class"<<endl;
                break;
         case 2: cout<<"Bussiness Class"<<endl;
                break;
         case 3:cout<<"Economy Class"<<endl;
                break;
       }
      cout<<endl;
      cout<<"Price: "<<price<<" KD\n";
      }
    the main problem section is in the ticket entry section

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Decide on whether you are using C or C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    16
    C++ with old standards which work on borland 5.02 (school requirement)

  4. #4
    Registered User
    Join Date
    Aug 2014
    Posts
    16
    does any one have an small idea why the program is giving troubles i ran the program twice and got these 2 outputs
    Binary files comparing problems-zqwco35-png

  5. #5
    Registered User
    Join Date
    Aug 2014
    Posts
    16
    does anyone have any idea for this question if yes and you are trying to find an answer atleast comment of this thread so that i will know help is on the way

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You need to follow the advice already given. First if you want to write a C++ program stop using the C-stdio functions like puts(), and never, Never, NEVER use gets() in either a C or C++ program. This dangerous function, which can never be used safely, should never ever be used. In a C++ program you should instead be using the C++ streams like cin, and never use any function that doesn't limit the number of characters it will retrieve when dealing with C-strings.

    [quote]
    C++ with old standards which work on borland 5.02 (school requirement)
    [/qoute]
    No, you are not using old "standards" since this compiler predates any C++ standard. I suggest you complain to your school about using antiquated tools like this and suggest they come into the present century and use a modern compiler.

    Jim

  7. #7
    Registered User
    Join Date
    Aug 2014
    Posts
    16
    it is no good as the software is required as per CBSE delhi and the entire country of kuwait follows it so i need to be on a borland compiler or not

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    • You should avoid mixing C/C++ I/O (cout/puts, cin/gets). Pick one and stick with it, preferably C++.
    • If you must use C I/O, use fgets instead of gets.
    • Const-correctness... member functions that do not modify the object in question should be declared const.
    • You are still needlessly exposing raw pointers to your class's private data in your get methods, at the very least you could perhaps make the returned pointers to const data.
    • If your output for code is blank, then it would seem that the bits of your program which copies the code value from the file must not be happening. Following that, it would therefore seem that the strcmp function is likely not finding a match. Be aware that you are also concatenating spaces on the end of your destination value so the value read from the file must also contain the appropriate number of spaces for the strcmp and be of the correct case (upper/lower) to find a match and work properly. To know this for sure, we would need to know what the contents of your files are.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Aug 2014
    Posts
    16
    Binary files comparing problems-p3vlbgy-png
    Image of the content of the file Flight_Record.dat ('|' added to show spaces)

  10. #10
    Registered User
    Join Date
    Aug 2014
    Posts
    16
    cin cannot take values after spaces so what should i use on the place of gets()?

  11. #11
    Registered User
    Join Date
    Aug 2014
    Posts
    16
    Could you elaborate points 3,4?

  12. #12
    Registered User
    Join Date
    Aug 2014
    Posts
    16
    what should i use on the place of gets() and puts() as cin does not read after a space

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Be patient.

    Thread bumping is frowned upon.
    Cross-posting is frowned upon.

    what should i use on the place of gets()
    One alternative was mentioned in post #8 (second bullet), although you should heed the advice in the first bullet about using C++ I/O.

    Rather than wait for a response, have you tried searching on your own?

    From the FAQ: FAQ > Obtain a string from the user (C++) - Cprogramming.com

  14. #14
    Registered User
    Join Date
    Aug 2014
    Posts
    16
    Oh you found the post i guess i am a bit nervous since i have to complete this project in 5 days

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing 2 txt files
    By norhos in forum C Programming
    Replies: 9
    Last Post: 03-13-2008, 06:18 AM
  2. Problems Comparing Char's
    By MrKnights in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2007, 01:49 PM
  3. comparing two files
    By afzan in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2005, 01:09 PM
  4. Comparing two files
    By pxleyes in forum C Programming
    Replies: 12
    Last Post: 09-24-2004, 08:06 AM
  5. Comparing Files
    By snowy101 in forum C++ Programming
    Replies: 3
    Last Post: 06-19-2002, 10:43 AM

Tags for this Thread