Hello everybody,
I wrote a program which works as a vehicle database for my homework. Features that desired for the program as follows.
- It must work with a data file. Both reading and writing (saving)
- It must have binary mode.
- It must be able to create new vehicles with its properties (km, year, brand, price). And save them to the data file.
- And it must present (show) the properties of a selected vehicle.
- A class named “Vehicle” must be created . And derived classes “cars”, ”trucks” also
- The code must contain inheritance.
- There must be polymorphism (a call to a member function will cause a different function to be executed according to type of object that invokes the function).
- The code must contain constructor and destructors used correctly.
- stl library must be used where it is possible.
And I wrote the code below. Everything was working fine. The only problem is about saving. When I want to save a new vehicle to the data file, it saves numbers in a weird font type. So when I want to show saved vehicles, it doesn't provide the information well.
I'm pretty new in c++ programming. I need your help to correct it. If you can correct the code that would be so useful for me to understand what was my mistake.
Thanks in advance
vehicles.cpp
#include "vehicles.h"
void read(){
ifstream input( "data.txt", ios::binary );
// copies all data into buffer
vector<unsigned char> buffer(istreambuf_iterator<char>(input), {});
//displays all data in binary format
for(int i=0;i<buffer.size();i++){
cout<<buffer[i];
}
return;
}
void menu(){
cout<<"\nMenu:"<<endl;
cout<<"1: Read data file in binary format"<<endl;
cout<<"2: Insert a Vehicle in binary mode"<<endl;
cout<<"3: Exit program"<<endl;
return;
}
int main(){
while(1){
menu();
int op;
cin>>op;
if(op==1){
read();
}
else if(op==2){
cout<<"\nEnter what you want to insert car or truck?"<<endl;
string type;
cin>>type;
if(type=="car"){
//open file in binary mode
ofstream wf("data.txt", ios::out | ios::binary);
if(!wf) { //If file cannot be opened show this message
cout << "Cannot open file!" << endl;
return 1;
}
cout<<"\nEnter details of truck in order (without , ) km, year, brand, price, peopleCapacity " << endl;
int a,b,c,d;
string e;
cin>>a>>b>>e>>c>>d;
Car C(a,b,e,c,d);
wf.write((char*)&C,sizeof(Car)); //Write it in binary mode
cout<<"\nYou have inserted car in file successfully ";
C.showPropertiesCar();
}
else{
//open file in binary mode
ofstream wf("data.txt", ios::out | ios::binary);
if(!wf) { //If file cannot be opened show this message
cout << "Cannot open file!" << endl;
return 1;
}
cout<<"\nEnter details of car in order (without , ) km, year, brand, price, Storage Capacity " << endl;
int a,b,c,d;
string e;
cin>>a>>b>>e>>c>>d;
Truck T(a,b,e,c,d);
wf.write((char*)&T,sizeof(Truck)); //Write it in binary mode
cout<<"\nYou have inserted car in file successfully ";
T.showPropertiesTruck();
}
}
else{
//Exit from program
break;
}
}
return 0;
}
vehicles.h
#ifndef VEHICLES_H
#define VEHICLES_H
#include<bits/stdc++.h>
#include <fstream>
using namespace std;
class Vehicle{
string brand;
int km,year,price;
public:
Vehicle(int km,int year,string brand,int price){ //Constructor for Vehicle
this->km=km;
this->year=year;
this->brand=brand;
this->price=price;
}
~Vehicle(){} //Deconstructor for Vehicle
void showProperties(){ //Displaying data of a vehicle
cout<<"Brand: "<<brand<<" year: "<<year<<" km: "<<km<<" price: "<<price;
}
};
class Car:public Vehicle{
int peopleCapacity;
public:
//Car Constructor will input inherited members from base class Constructor and peopleCapacity here
Car(int km,int year,string brand,int price,int peopleCapacity):Vehicle(km,year,brand,price){
this->peopleCapacity=peopleCapacity;
}
~Car(){} //Deconstructor for Car
void showPropertiesCar(){
showProperties(); //Calls base class to display its members and then displays car members
cout<<" peopleCapacity: "<<peopleCapacity<<endl;
}
};
class Truck:public Vehicle{
int storageCapacity;
public:
//Truck Constructor will input inherited members from base class Constructor and peopleCapacity here
Truck(int km,int year,string brand,int price,int storageCapacity):Vehicle(km,year,brand,price){
this->storageCapacity=storageCapacity;
}
~Truck(){} //Deconstructor for Truck
void showPropertiesTruck(){ //Calls base class to display its members and then displays truck members
showProperties();
cout<<" storageCapacity: "<<storageCapacity<<endl;
}
};
#endif