I am new to c++ and managed to write this much of code, i dont know what should I write inside of Vehicle and get_line functions. Could you point me how to do it? or provide a pseudocode please? I am confused a bit:
#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
#include <fstream>
usingnamespace std;
class Vehicle
{
public:
Vehicle();
void get_line(ifstream&);
void display_data();
private:
char make[11];
char model[21];
char numberPlate[8];
int registration_date[9];
char registeredKeeper[21];
int taxiLicence[11];
int numWheels[9];
double weight[9];
bool is_taxi;
};
int main()
{
Vehicle temp;
vector<Vehicle> vehicle;
ifstream infile("vehicles.txt");
//Provide simple error checking
if(!infile.is_open())
{
cout << "\n\aError! File could not be opened!";
cout << "\nFile may have been moved, renamed, or deleted...";
exit(1);
}
//Load the text file into a vector of 'vehicle' objects
while(infile)
{
//Load the .txt file 'line-at-a-time'
temp.get_line(infile);
vehicle.push_back(temp);
}
//We are done with the file i/o, so we can go ahead and clean-up the ifstream object
infile.close();
//Display file contents to the user
for(int i=0, size=vehicle.size(); i<size; i++)
{
vehicle[i].display_data();
}
return 0;
}
void Vehicle() {
} //what should I put here?
void Vehicle::get_line(ifstream&)
{
} //and what should I put here?
void Vehicle::display_data() //is this done correctly?
{
cout<<make;
cout<<"";
cout<<model;
cout<<"";
cout<<weight;
cout<<"";
cout<<numberPlate;
cout<<"";
cout<<registration_date;
cout<<"";
cout<<registeredKeeper;
cout<<"";
cout<<taxiLicence<<"\n";
}
That would depend on the layout of the file and what you want to read in. The constructor ought to establish at least some sensible default values for the class.