My program works fine hours ago and suddenly doesn't work anymore, Iam really sorry to post all codes but I am totally confused with what went wrong there.
#include <iostream>
#include <fstream>
using namespace std;
class Car
{
protected:
string carmodel;
string serialnumber;
string description;
int yearmade;
public:
Car ()
{
}
};
class Service
{
protected:
string serviceinfo;
int lmonth, nmonth,lday, nday, lyear, nyear, mbetw;
public:
Service ()
{
}
void udate(void)
{
nday = lday;
nmonth = lmonth;//is this necessary
nyear = lyear;
nmonth = lmonth + mbetw;
while (nmonth > 12)
{
nyear = nyear + 1;
nmonth = nmonth - 12;
}
}
};
class Autobody: public Car, public Service
{
public:
Autobody()
{
}
static void file_write(int n);
static void serial_file_read(string serial);
static void date_file_read(int d, int m, int y);
friend istream &operator >> (istream &in , Autobody &iinfo);
friend ostream &operator << (ostream &out , Autobody &oinfo);
};
void Autobody:: file_write(int n)
{
Autobody *a1 = new Autobody[n];
for (int i = 0; i < n; i++)
{
cin >> a1[i];
a1[i].udate();
}
ofstream fdata("cars.txt", ios::app);
fdata.seekp(0, ios::end);
fdata.write((char*) a1, n*sizeof(Autobody));
fdata.close();
delete []a1;
}
void Autobody:: date_file_read(int d, int m, int y)
{
int n;
ifstream fdata2("cars.txt", ios::in);
if (!fdata2)
{cout << "Nothing in the file,please run again and create a file first"<<endl;
exit(1);
}
fdata2.seekg(0,ios::end);
n = fdata2.tellg() / sizeof(Autobody);
Autobody *a2 = new Autobody[n];
fdata2.seekg(0,ios::beg);
fdata2.read((char*) a2 , n*sizeof(Autobody));
for (int i = 0 ; i < n ; i++)
{
if ((d == a2[i].nday) & (m == a2[i].nmonth) & (y == a2[i].nyear))
cout << a2[i];
}
fdata2.close();
delete []a2;
}
void Autobody::serial_file_read(string serial)
{
int d;
ifstream filed2("cars.txt", ios::in);
if (!filed2)
{cout << "Nothing in the file,please run again and create a file first"<<endl;
exit(1);
}
filed2.seekg(0,ios::end);
d = filed2.tellg() / sizeof(Autobody);
Autobody *a2 = new Autobody[d];
filed2.seekg(0,ios::beg);
filed2.read((char*) a2 , d*sizeof(Autobody));
for (int i = 0 ; i < d ; i++)
{
if (strcmp(serial.c_str(),a2[i].serialnumber.c_str()) ==0)
cout << a2[i];
}
filed2.close();
delete []a2;
}
istream &operator >> (istream &in , Autobody &info)
{
cout << " Input car model: ";
in.ignore();
getline(cin,info.carmodel);
cout << " Input licence plate number: ";
in >> info.serialnumber;
cout << " Input year made: ";
in >> info.yearmade;
cout << " Input car description: ";
in.ignore();
getline(cin, info.description);
cout << " Input service start date(Day month year): " ;
in >> info.lday >> info.lmonth >> info.lyear ;
cout << " Input months between two service: ";
in >> info.mbetw;
cout << " Input service information: ";
in.ignore();
getline(cin, info.serviceinfo);
cout << " Info is appended"<<endl;
return in;
}
ostream &operator << (ostream &out , Autobody &info)
{
out << "Car model: "<<info.carmodel<<endl;
out << "Serial number: " << info.serialnumber << endl;
out << "Car description: " << info.description << endl;
out << "Service information: " << info.serviceinfo << endl;
out << "Last service date and next service date(day month year): " << info.lday << "/" << info.lmonth << "/" << info.lyear<<"-"<<info.nday << "/" << info.nmonth << "/" << info.nyear <<endl;
return out;
}
void displaymenu(){
cout<<endl;
cout<< "1.Create new car record"<<endl;
cout<< "2.Search by serial number"<<endl;
cout<< "3.Search by next service date"<<endl;
cout<< "0.Exit"<<endl;
cout<<endl;
}
int main ()
{
int option;
int day, month, year;
string serialnumber;
cout<<"This is the car managing system,select your function press 0-3"<<endl;
while(true)
{
displaymenu();
cout << "Choose the option: ";
cin >> option;
cout <<endl;
switch(option){
case 1:
Autobody::file_write(1);
break;
case 2:
cout << "Enter serial number: ";
cin >> serialnumber;
cout <<endl;
Autobody::serial_file_read(serialnumber);
break;
case 3:
cout << "Enter the date(day month year): ";
cin >> day >> month >> year;
cout << endl;
Autobody::date_file_read(day,month,year);
break;
case 0:
cout << "Thanks for using!" << endl;
break;
default:
cout << "You have to choose between 0 - 3 \t"<<endl;
break;
}
};
return 0;
}
The error shows: project_test_again(1945,0x1000e2d40) malloc: * error for object 0x10000000c: pointer being freed was not allocated project_test_again(1945,0x1000e2d40) malloc: * set a breakpoint in malloc_error_break to debug
Aucun commentaire:
Enregistrer un commentaire