Binary files Search Problem

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
1
2
3
4
5
6
7
8
9
10
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;}
  };


Ticket Class
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
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


  };                

Ticket entry
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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();
   }
 
 
  }


Display
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
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 is in the entry function at it end
You've lost many of the guarantees C++ can provide by writing the code as you have done. Little things like the use of const, and remembereing that C strings have a size (not just a pointer to the start), and so on.

Your use of the file in that way removes any opprotunity to use improved data structures or algorithms to help you. You're restricted to doing unbuffered linear searches.

Also, you've ignored some basics of objects, like the observation that they clean up on destruction. If you had realised that, you would know that you don't need all those d2.close() calls before return in the loop.

Anyway, there's no obvious reason why it's not working. But you are exposed to buffer overruns. I suggest you run the program in a debugger and check the code/day comparisons and ensure that the strings being compared are shorter than the buffers you have reserved for them.
i am Not experienced to with debuggers do you have any links for learning to use debuggers. I use borland c++ 5.02 compiler
You probably should be using Turbo Debugger, which was one of the best in its day.

Build the program for debug, then just start it with tdd32.exe (as I recall).

Alternatively, you can install an Express edition of Visual Studio and use that.

Or, you can just use print statements to print the content of things temporarily.
Last edited on
i am not aware on how to use debuggers soo any tutorials?
Have you tried using web search? Look for "turbo debugger"

I can't really help you with this, maybe someone else can. The Borland/Turbo stuff is pretty old, and I can't remember much about it. Furthermore, I tend not to develop on Windows these days.
Topic archived. No new replies allowed.