0

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));
}
Mac
  • 14,085
  • 9
  • 61
  • 77
S Chow
  • 11
  • 1
    You might want to reformat the code so it is readable using the 'code' block, and you also might want to add the 'homework' tag to your tags. – jacknad Aug 24 '10 at 12:21
  • 1
    Your code is very unclear. Name your variables correctly. Use new lines in your code correctly. Also, you should drop the dos.h and conio.h dependencies (get rid of clrscr and gotoxy statements in your code) first get the functionality working only then work on the presentation of the output. – Elf King Aug 24 '10 at 12:38
  • 1
    Why void main() ? It should be int. http://stackoverflow.com/questions/204476/what-should-main-return-in-c-c – DumbCoder Aug 24 '10 at 12:43
  • And why and and... Aren't you using C++ and STL? Stick to , , and so on. – jweyrich Aug 24 '10 at 12:57

2 Answers2

0

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
  • 536
  • 4
  • 15
  • Please give the reason for why is it happening i.e. after 4 records why is it not accepting any more> – S Chow Aug 24 '10 at 12:30
  • 2
    What is your test data? How does it "stop accepting"? By crashing? By hanging? By shaking its head defiantly? – Matt_JD Aug 24 '10 at 12:48
  • 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 Aug 24 '10 at 12:53
  • 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 Aug 25 '10 at 13:42
  • 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 Aug 25 '10 at 13:50
  • You are right about cout< – S Chow Aug 25 '10 at 13:51
  • Other than changing the order, that all goes in fine for me. – Matt_JD Aug 25 '10 at 14:15
  • 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 Aug 25 '10 at 14:45
  • 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 Aug 28 '10 at 09:26
  • 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 Aug 29 '10 at 02:15
  • 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 Aug 29 '10 at 02:19
0

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 How do I flush the cin buffer?

Community
  • 1
  • 1
bjskishore123
  • 5,847
  • 9
  • 37
  • 65
  • 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 are 1010,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 Aug 25 '10 at 13:49
  • I am unable to enter input data. Some fields are getting skipped due to the characters present in input stream cin. – bjskishore123 Aug 26 '10 at 08:12