views:

122

answers:

2

in c++ i have written a simple program, which accepts 4 to 6 records and then do not accept any more when airline_no is same i.e. 1? The source code is:

#include<fstream.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
#include<stdlib.h>
#include<process.h>
#include<stdio.h>
int lno;
struct airln {int airline_no,routeno,dep_time,arr_time;
    char port_d[15],port_a[15],week_day[10];  }r2;

class route
{
 public:
    void getroute()
    {

        cout<<"\n\tRoute no.: ";
        cin>>r2.routeno;cout<<"\n";
        cout<<"\tDay of Departure: ";
        gets(r2.week_day);cout<<"\n";
                cout<<"\tAirport for departure: ";
        gets(r2.port_d);cout<<"\t";
        cout<<"Departure Time: ";
        cin>>r2.dep_time;cout<<"\n";
        cout<<"\tAirport for arrival: ";
        gets(r2.port_a);cout<<"\t";         
                cout<<"Arrival Time: ";
        cin>>r2.arr_time;cout<<"\n";
        cout<<"\n\tAirline no.: ";
        cin>>r2.airline_no;
    }
    void display_route()
    {
    cout<<"\n    Route No : ";cout<<r2.routeno;
    cout<<"\n    ";
    cout<<r2.port_d;cout<<"\t";
    cout<<r2.dep_time;cout<<"\t\t";
    cout<<r2.port_a;cout<<"     \t";
    cout<<r2.arr_time;cout<<"\t\t";
    cout<<r2.week_day;cout<<"\n";
    }
}r3;
void main()
{
    clrscr();
    int airlnno,rtnodel,cntr;
    char airlinename[30];
    ifstream fin,fin1;
    ofstream fout;

    do
    {
    cout<<"\t1. Insert Data.\n";
    cout<<"\t2. View Data.\n";
    cout<<"\t3. Exit.\n";
    cout>>"\tEnter Choice : ";cin>>cntr;
    switch(cntr)
    {

           case 1:r3.getroute();
                   fout.open("testdata.dat",ios::app);
                   if(!fout)
                   {
                        gotoxy(25,10);
                        cout<<"No file exists or file can\'t be opened\n";
                        gotoxy(25,22);
                        cout<<"Please Press Any Key to Continue.......";
                        getch();
                        clrscr();
                       }
                   fout.write((char *) &r2,sizeof(r2));
                   fout.close();
                   clrscr();
                   break;
        case 2:fin.open("testdata.dat",ios::in);
               if(!fin)
               {
                gotoxy(25,20);
                cout<<"No file exists or file can\'t be opened\n";
                gotoxy(25,22);
                cout<<"Please Press Any Key to Continue.......";
                getch();
                clrscr();
                break;
               }
               cout<<"\n    Dep-Airport\t";
               cout<<"Dep-Time\t";
               cout<<"Arr-Airport\t";
               cout<<"Arr-Time\t";
               cout<<"Week Day\n";
               fin.read((char *) &r2,sizeof(r2));
               while(!fin.eof())
               {
            r3.display_route();
            fin.read((char *) &r2,sizeof(r2));
               }
               fin.close();
               gotoxy(25,22);
               cout<<"Please Press Any Key to Continue.......";
               getch();
               clrscr();
               }
               } while(!(cntr==3));
}
A: 

Do you have to use files for this? You could always use std::map and keep the airline_no as the key.

I'd also avoid using the global struct and class.

Edit: k, I've got a problem and found that the 1050 is the problem. I can move it to an earlier record and it will stop there too. I think the problem is that 1050 is somehow triggering End of File. Try the following replacement lines to open the file in binary:

fout.open("testdata.dat",ios::app | ios::binary);
fin.open("testdata.dat",ios::in | ios::binary);

instead of:

fout.open("testdata.dat",ios::app);
fin.open("testdata.dat",ios::in);

Edit: Just had a check and 1050 converts to 041A, which translates to the characters End of Transmission and then.... SUB or EOF. I think that's your problem and in binary mode this shouldn't be a problem any more.

Matt_JD
Please give the reason for why is it happening i.e. after 4 records why is it not accepting any more>
S Chow
What is your test data? How does it "stop accepting"? By crashing? By hanging? By shaking its head defiantly?
Matt_JD
After removing some bits to get it to run on my computer, it happily ran well over 4 times.Also, shouldn't "cout>>"\tEnter Choice : ";cin>>cntr;" be "cout<<..."?
Matt_JD
Sorry I have not been able to explain the problem. What I asked was in the file testdata.dat record can not be written for more than 4 times with same airline_no. Although coice 1 of the code does not show any error giving an impression that data has been written, but when coice 2 for display is used only 4 records are shown. Please help understand.
S Chow
Hi, I just ran it on my computer and had every value set as 1. I added 7 records full of 1s and when I tried option 2 I had 7 records displayed. Is this not what happens for you? EDIT: 7, not 6. I can't count.
Matt_JD
You are right about cout<<,I have not explained the problem well. What I meant, in file testdata.dat record can not be written for more than 4 times with same airline_no. Although coice 1 of the code does not show any error giving an impression that data has been written, but when coice 2 for display is used only 4 records are shown. Please help understand.My test data are1010,New Delhi,1000,Mumbai,1200,All Days,1;1020,New Delhi,1000,Mumbai,1200,All Days,1;1030,New Delhi,1000,Mumbai,1200,All Days,1;1040,New Delhi,1000,Mumbai,1200,All Days,1;1050,New Delhi,1000,Mumbai,1200,All Days,1;
S Chow
Other than changing the order, that all goes in fine for me.
Matt_JD
k, I've got a problem and found that the 1050 is the problem. I can move it to an earlier record and it will stop there too.I think the problem is that 1050 is somehow triggering End of File.Try the following replacement lines to open the file in binary: fout.open("testdata.dat",ios::app | ios::binary); fin.open("testdata.dat",ios::in | ios::binary);instead of: fout.open("testdata.dat",ios::app); fin.open("testdata.dat",ios::in);
Matt_JD
So I hope u have understood the problem -- now can we find the correct explanation. "Somehow triggering end of file" is confusing.I have found a solution by introducing another field as "slno" which I am updating sequentialy and now i can enter as many number of records with same airline_no as i want. But why is it happening like that? i want help!!!
S Chow
The number 1050 translates to the hex 041A and this is written to your file correctly. However when you are reading, the 1A character translates to the ASCII character for the end of file. This means that when you are reading the data, your stream is getting to that character and then thinks you've found the end of the file and stops. You can overcome this by opening the file in a binary mode as I have edited my answer to show.
Matt_JD
Just to clarify, it is nothing to do with adding airlines with the same number. It is because you use the number 1050 for the route number. Try using the following input data and you'll find that it stops at the second rather than 4th.1010,New Delhi,1000,Mumbai,1200,All Days,1;1020,New Delhi,1000,Mumbai,1200,All Days,1;1050,New Delhi,1000,Mumbai,1200,All Days,1;1040,New Delhi,1000,Mumbai,1200,All Days,1;1030,New Delhi,1000,Mumbai,1200,All Days,1;
Matt_JD
A: 

You need to clear cin after receiving each input each time. Probably at the begining of do while loop. cntr value may be taken from previously entered values. If it happens to be 3, it exits while loop and does not accept any further records.

Try out cin.clear() or cin.ignore() functions at the begining of do while loop.

You can refer http://stackoverflow.com/questions/257091/how-do-i-flush-the-cin-buffer

bjskishore123
Sorry I have not been able to explain the problem. What I asked was in the file testdata.dat record can not be written for more than 4 times with same airline_no. Although coice 1 of the code does not show any error giving an impression that data has been written, but when coice 2 for display is used only 4 records are shown. Please help understand.My test data are1010,New Delhi,1000,Mumbai,1200,All Days,1;1020,New Delhi,1000,Mumbai,1200,All Days,1;1030,New Delhi,1000,Mumbai,1200,All Days,1;1040,New Delhi,1000,Mumbai,1200,All Days,1;1050,New Delhi,1000,Mumbai,1200,All Days,1;
S Chow
I am unable to enter input data. Some fields are getting skipped due to the characters present in input stream cin.
bjskishore123