那位来帮我解释一下..感谢!!!!!!!!!!

zezeze 2005-11-14 10:32:50
程序如下..
有一处错误..不理解错那了..我用打开方式位的方法打开文件
请那位帮我解释一下谢谢!
#include <iostream.h>
#include <fstream.h>
#include <string>

class NRailways
{
private:
char pnr[6];
char pname[30];
char date_departure[9];
char to[40];
char from[40];
char status;
int No_of_Seats;
public:
NRailways()
{
pnr[0]='\0';
pname[0]='\0';
date_departure[0]='\0';
to[0]='\0';
from[0]='\0';
status=' ';
No_of_Seats=0;
}

void print()
{
cout<<"\n\n PNR Number :\t"<<pnr;
cout<<"\n Passenger Name :\t"<<pname;
cout<<"\n Date of Departure :\t"<<date_departure;
cout<<"\n Destination To :\t"<<to;
cout<<"\n From :\t"<<from;
cout<<"\n Status of Booking :\t"<<status;
cout<<"\n # of Seats Reserved :\t"<<No_of_Seats;
}
void get()
{
cout << "Enter the PNR Number:\t";
cin >> pnr;
cout << endl << "Enter Passenger Name:\t";
cin>>pname;
cout << endl << "Enter Date of Departure: \t";
cin >> date_departure;
cout << endl << "From:\t";
cin>>from;
cout << endl << "To:\t ";
cin>>to;
cout << endl << "Enter Status of Booking\t";
cin >> status;
cout << endl << "Enter # of Seats Reserved:\t";
cin >> No_of_Seats;
}
char getstatus()
{
return status;
}
char *getpnr()
{
return pnr;
}
void setstatus()
{
char choice;
cout<<"\n\n Do you want to cancel the Booking?\t";
cin>>choice;
if(choice=='y' || choice=='Y')
status='R';
}
};
int main()
{
int ch;
while(1)
{
cout<< "\n \n Northern Railway System\n";
cout<< "\n 1. Enter Passenger Details\n";
cout<< "\n 2. Query Passenger Details \n";
cout<< "\n 3. Cancel the Booking \n";
cout<< "\n 4. Print Confirmed Booking List\n";
cout << "\n Enter your choice (0-4)\t";
cin>>ch;

if(ch==1)
{
NRailways object;
fstream ofile("booking1.txt",ios::app);
cout << "Enter Passenger Details " << endl;
object.get();
ofile.write((char *)&object,sizeof(object));
ofile.close();
}
if(ch==2)
{
NRailways object;
ifstream ifile("booking1.txt");
char pnr[6];
cout<<"\n\n Enter the PNR Number:\t";
cin>>pnr;
ifile.seekg(0,ios::beg);
ifile.read((char *)&object, sizeof(object));
while(ifile) //Read Till The End Of The File
{
char temp[6];
strcpy(temp,object.getpnr());
if(strcmp(temp,pnr)==0)
{
object.print();
break;
}
ifile.read((char *)&object, sizeof(object));
}
ifile.close();
}
if(ch==3)
{
NRailways o;
fstream ie("booking1.txt",ios::in|ios::out);
char pnr[6];
int pos;
cout<<"\n\n Enter the PNR Number:\t";
cin>>pnr;
ie.read((char *)&o, sizeof(o));
while(ie)
{
char temp[6];
pos=ie.tellg();
strcpy(temp,o.getpnr());
if(strcmp(temp,pnr)==0)
{
o.setstatus();
ie.seekp(pos-sizeof(o),ios::beg);
ie.write((char*)&o,sizeof(o));
ie.close();
break;
}
else
ie.read((char *)&o, sizeof(o));
}
ie.close();
}
if(ch==4)
{
NRailways object;
ifstream ifile("booking1.txt");
ifile.read((char *)&object, sizeof(object));
while(ifile) //Read Till The End Of The File
{
if(object.getstatus()=='C'||object.getstatus()=='c')
object.print();
ifile.read((char *)&object, sizeof(object));
}
ifile.close();
}
if(ch==0)
break;
}
return 0;
}
...全文
108 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
pbdwadr 2005-12-04
  • 打赏
  • 举报
回复
你是不是指死循环的问题??

int main()
{
int ch;//这里不可以,因为ch 已经被第一次付值 了,所以应该初始化,改为ch=0就可以了
while(1)
{...

benqmm 2005-11-15
  • 打赏
  • 举报
回复
你应该先整理一下吧!没看懂你说的什么
oosky2004 2005-11-15
  • 打赏
  • 举报
回复
晕。。。。????
DanceFire 2005-11-15
  • 打赏
  • 举报
回复
什么叫有一处错误?也不说清楚了。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧