2016-11-30 9 views
-2

完全なコードはpastebinです。 列車データベースがあり、列車番号を入力してチケットを予約します。関数updt_tickは列車の名前、元と目的地の値を乗客の予約オブジェクトにコピーする必要がありますが、5文字目以降はコピーされません。 updt_tick関数は87行目にあります。関数のコードサンプルは次のとおりです.Trainデータベースはユーザーによって入力されます。重要な場合はcodeblocksを使用しています。strcpyは文字列sthをコピーしています。このエラーを解決するには?

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(); 
} 

列車のクラスがある - >

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

public: 
    void getdetail() 
    { 
     cout<<"Enter the details as follows\n"; 
     cout<<"Train no:"; 
     cin>>tno; 
     cin.ignore(); 
     cout<<"Train name:"; 
     gets(tname); 
     cout<<"Train Source Station:"; 
     gets(source); 
     cout<<"Tarin Destination Station:"; 
     gets(dest); 
     cout<<"Fare per seat in train :"; 
     cin>>PerSeatFare; 
    } 
    void showdetail() 
    { 
     cout<<tno<<"\t"<<tname<<"\t"<<source<<"\t"<<dest<<"\t"; 
     cout<<PerSeatFare; 
    } 
}t; 

RESERVクラスがある - > `

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; 
    } 
}; 
+0

これは何ですか? –

+0

't'、' bp'、 'tname'、' tno'が何であるかわかりません。 [MCVE](http://stackoverflow.com/help/mcve) –

+0

を提供してください。これを 'C++'と 'string'とタグ付けしてください。これは 'std :: string'を意味します。なぜ 'std :: string'を使わないのですか? – PaulMcKenzie

答えて

1

イムないがあるので、あなたが、5つの文字を読むために管理したのかどうかはわかりあなたのコードに問題があります。列車の詳細を格納するために使用するファイルは、 "train.dat"ではなく "trainS.dat"です。

+0

ああ、たわごとのたわごと。私は深く、このようなnoob-ishエラーのためにお詫び申し上げます。 –