0

output page

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.

output page

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
  • The file `data.txt` might have a `.txt` suffix, but that doesn't automatically make it a text file. Instead you open it in *binary* mode and uses `write` to write the raw data of you objects. And your object contains `std::string` which can't be written in such raw binary mode. If you want to write text, you need to use the normal text output primitives (like `<<`) on a text-mode file. This should really be well-documented in any [decent text book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), tutorial or class. – Some programmer dude Jun 07 '21 at 10:05
  • And please read [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) Doing it a header file makes it at least a magnitude worse. Also please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Jun 07 '21 at 10:06
  • `wf.write((char*)&T,sizeof(Truck));` - that won't work with members like `std::string`. You need to serialize it "manually", field by field (you should also think about your binary file format, like how you encode variable-length strings, etc). – rustyx Jun 07 '21 at 10:10

0 Answers0