-2

My complete code is at pastebin.

There is a train database , and user enters train number to book a ticket.The function updt_tick should copy the values of train's name,source and destination into passenger's reservation object. But the problem is that 5th character onwards are only being copied. Here is the function's code sample.Train database is entered by user.

void updt_tick()
{
    fstream f;
    f.open("train.dat",ios::in | ios::binary);
    while(f.read((char*)&t,sizeof(t)))
    {
        if (tno==t.tno)
        {
            strcpy(bp,t.source);
            strcpy(dest,t.dest);
            strcpy(tname,t.tname);
            amt=SeatNum*t.PerSeatFare;
            break;
        }
    }
    f.close();
}

The train class is ->

class train
{
public:
    int tno;
    char tname[100];
    char source[100];
    char dest[100];
    int PerSeatFare;

public:
    void getdetail();
    void showdetail();
}t;

The reserv class is -> `

class reserv            
{
//Assume that cust select train according to his source and destination. 
public:
    int pnr;
    int tno;
    char tname[100];
    char pnames[10][100];
    int ages[10];
    int SeatNum;
    int i;
    int d,m,y;
    float amt;
    char bp[100],dest[100];
    void updt_tick()
    {
        fstream f;
        f.open("train.dat",ios::in | ios::binary);
        while(f.read((char*)&t,sizeof(t)))
        {
            if (tno==t.tno)
            {
                strcpy(bp,t.source);
                strcpy(dest,t.dest);
                strcpy(tname,t.tname);
                amt=SeatNum*t.PerSeatFare;
                break;
            }
        }
        f.close();
    }

public:
    void getresdet()
    {
        cout<<"Enter the details as follows\n";
        cout<<"Train no:";
        cin>>tno;
        cout<<"No of seats required:";
        cin>>SeatNum;
        cin.ignore();
        for(i=0; i<SeatNum ; i++)
        {
            cout<<"Passenger name:";
            gets(pnames[i]);
            cout<<"Passenger age:";
            cin>>ages[i];
            cin.ignore();
        }
        cout<<"Date of travel:";
        cin>>d>>m>>y;
        cout<<"Details Accepted\n";
        pnr=rand();
        updt_tick();
    }
    void showresdet()
    {
        cout<<"Pnr no:"<<pnr;
        cout<<"\nTrain no:"<<tno;
        cout<<"\nTrain name:";
        puts(tname);
        cout<<"Boarding point:";
        puts(bp);
        cout<<"Destination pt:";
        puts(dest);
        cout<<"No of seats reserved:"<<SeatNum;
        for(i=0; i<SeatNum; i++)
        {
            cout<<"Passenger name:";
            puts(pnames[i]);
            cout<<"Passenger age:"<<ages[i];
        }
        cout<<"\nDate of reservation:"<<d<<"-"<<m<<"-"<<y;
        cout<<"\nYou must pay:"<<amt<<endl;
    }
    int getpnr()
    {
        return pnr;
    }
};

Edit: There was nothing wrong with strcpy or any other code. I made the foolish mistake of giving the file name as "train.dat" instead of "trains.dat:.

Abhishek Ranjan
  • 498
  • 3
  • 17
  • what is that `t`? – Mohamed Moanis Nov 30 '16 at 15:10
  • we don't know what `t`,`bp`, `tname` or `tno` are. Please provide a [MCVE](http://stackoverflow.com/help/mcve) – Ælex Nov 30 '16 at 15:10
  • You tagged this as `C++` and `string`. That means `std::string`. Why are you not using `std::string`? – PaulMcKenzie Nov 30 '16 at 15:11
  • Also, we need to know what `t` is. If it is a non-POD type, none of this code is guaranteed to work as you cannot create `t`'s using raw binary reads. – PaulMcKenzie Nov 30 '16 at 15:12
  • I included my whole code so that to avoid this , anyways, t is a global varaiable of train class – Abhishek Ranjan Nov 30 '16 at 15:29
  • Please see my code from pastebin link, its a small project. @MohamedMoanis – Abhishek Ranjan Nov 30 '16 at 15:30
  • In the code that you did not include in your post, t is a global variable that is sometimes shadowed by a local variable by the same name. This makes it harder to sort out your code. You don't want to do this. If you want someone else to look at your code, you really don't want to do this. Also, t being a global means that there is not much reason to think that the problem is in the extract that you posted, unless you have addition information you got from using the debugger. I foresee a code rewrite and /or a long debugger session in your future. – Avi Berger Nov 30 '16 at 15:33
  • @AviBerger Thank you for your useful inputs sir, I have just started to use stackoverflow correctly. I will follow your advises. RIght now, I am going to check after defining t as a local variable . – Abhishek Ranjan Nov 30 '16 at 15:36
  • I ran the code again, with t as a local variable, the problem persists.an d for some reason, I am now getting all garbage values in all the varible in which I'm using strcpy to get some string copy-pasted. – Abhishek Ranjan Nov 30 '16 at 15:38
  • @ierlich You should be voting to close this nonsense, not editing it. – user207421 Jun 27 '17 at 22:12
  • @EJP Why do you want someone's mistake to be deleted ? It may help others :( – Abhishek Ranjan Jun 29 '17 at 05:00

1 Answers1

1

Im not sure how did you manage to read 5 characters, because there is a problem is in your code. The file which you use to store train details is "trainS.dat", not "train.dat".

Kiko
  • 319
  • 1
  • 4
  • 16