这是我编的一个程序,我想增加一些算法,怎样才能把“化验单”中的内容写到一个txt文件中?
void select()
{
int a;
cout<<"请选择要检查的项目:"<<endl;
cout<<"1.尿液常规检查 2.血液常规检查 3.两项都检查"<<endl<<endl;
cin>>a;
switch(a)
{
case 1:{
string Col;
float Ph;
double SG;
char BLD;
char Pro;
cout<<"请输入病人的尿常规检查数据:"<<endl;
cin>>Col>>Ph>>SG>>BLD>>Pro;
Urine U;
U.seturine(Col,Ph,SG,BLD,Pro);
UrineExam urinedata(U); /*构造UrineExam类的对像urineexam*/
urinedata.showdata(); /*输出尿常规*/
cout<<"PAY ATTENTION!"<<endl;
urinedata.Compare(); /*将实际值与参考值进行比较*/
break;
}
case 2:{
float RBC;
float WBC;
int PLT;
int HGB;
cout<<"请输入血常规检查的数据:"<<endl;
cin>>RBC>>WBC>>PLT>>HGB;
Blood B;
B.RBC=RBC;
B.WBC=WBC;
B.PLT=PLT;
B.HGB=HGB;
BloodExam blooddata(B); /*构造BloodExam类的对象blooddata*/
blooddata.showdata(); /*输出血液常规*/
cout<<"PAY ATTENTION!"<<endl;
blooddata.compare();
break;}
case 3:{
string Col;
float Ph;
double SG;
char BLD;
char Pro;
float RBC;
float WBC;
int PLT;
int HGB;
cout<<"请输入病人的尿常规检查数据:"<<endl;
cin>>Col>>Ph>>SG>>BLD>>Pro;
cout<<"请输入血常规检查的数据:"<<endl;
cin>>RBC>>WBC>>PLT>>HGB;
Blood B;
B.RBC=RBC;
B.WBC=WBC;
B.PLT=PLT;
B.HGB=HGB;
BloodExam blooddata(B); /*构造BloodExam类的对象blooddata*/
blooddata.showdata(); /*输出血液常规*/
cout<<"PAY ATTENTION!"<<endl;
blooddata.compare();
Urine U;
U.seturine(Col,Ph,SG,BLD,Pro);
UrineExam urinedata(U); /*构造UrineExam类的对像urineexam*/
urinedata.showdata(); /*输出尿常规*/
cout<<"PAY ATTENTION!"<<endl;
urinedata.Compare(); /*将实际值与参考值进行比较*/
break;}
default:cout<<"error!"<<endl;
}
}
int main()
{
ofstream ocout;
ocout.open("test.txt");
string name;
string ID;
int age;
string sex;
ocout<<"请输入病人的基本信息:"<<endl;
cin>>name>>ID>>age>>sex;
Exam exam(name,ID,age,sex);
exam.showinfor();
cout<<endl;
select();
cout<<endl;
return 0;
}
bloodexam 都已定义。
[解决办法]
自己看看如何写文件不就行了。
打开一个文件,写文件,然后关上。C++的标准库里都有,形式和你已经用的标准输出流是一样的啊。