这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct car
{
string name, model;
int year;
};
void search_car(int CarYear)
{
cout<<"1";
ifstream in;
cout<<"2";
car c1;
cout<<"3";
in.open("Cars.txt",ios::binary|ios::in);
cout<<"4"<<endl;
while(!in.eof())
{
cout<<" 5";
in.read((char *) &c1, sizeof(car));
cout<<" 6.Car Year: "<<c1.year<<endl;
if(c1.year == CarYear)
{
cout<<" 7>>> ";
cout<<c1.name<<" "<<c1.model<<" "<<c1.year;
cout<<" <<<8"<<endl;
}
}
cout<<" 9";
in.close();
cout<<" 10";
}
void main()
{
car c[100];
int carNum, menuAct = 0, CarYear = -1, cycle = 1;
ofstream out;
while (cycle == 1)
{
//clrscr();
cout<<endl<<endl<<"1.Enter New car"<<endl<<"2.Search"<<endl<<"3.Exit"<<endl;
cin>>menuAct;
cout<<" Menu Action: "<<menuAct<<endl;
if(menuAct == 1)
{
cout<<"Enter Num OF Cars: ";
cin>>carNum;
out.open("Cars.txt",ios::binary|ios::out|ios::app);
for(int i = 0; i < carNum; i++)
{
cout<<"Enter Name OF Car: ";
cin>>c[i].name;
cout<<"Enter model OF Car: ";
cin>>c[i].model;
cout<<"Enter year OF Car: ";
cin>>c[i].year;
out.write((char *) &c[i], sizeof(car));
}
out.close();
}
else if(menuAct == 2)
{
cout<<"Enter Car Year: ";
cin>>CarYear;
cout<<" 0";
//cout<<" Y: "<<CarYear;
search_car(CarYear);
cout<<" 11";
//menuAct = 0;
}
else if(menuAct == 3)
{
cycle = 0;
}
}
}
错误:
http://s3.picofile.com/file/7580464836/cpp_err11.jpg
发生了什么? 我使用了一些 cout 来跟踪正在发生的事情,并且代码在 10 号处停止。
最后一辆车也打印了两次!!!