Hi,
I have a problem, I dont know how to import data into a single array and then output it to the screen. I am quite new to c++ and would approciate your help.
Here is information:


When a vehicle has been manufactured, it has the following attributes: make (e.g. \Ford"), model(e.g. \Focus"), a number of wheels and a weight in kilograms. In order to sell it, the vehicle must be registered by the DVLA, which allocates it a number plate consisting of seven alphanumeric characters and records the date on which it was registered and the name of the registered keeper.If the vehicle is to be used as a taxi, then, as well as vehicle registration, further taxi registration is required. It must be registered by the local authority, which allocates it a ten-digit taxi licence number.

vehicles.txt le. It contains details about a number of vehicles, some of which may be registered by the DVLA and some of which may also be registered as taxis.The denitions of the elds in each record of the le are described in the table below.

- I need to write Vehicle class, RegisteredVehicle class and RegisteredTaxi class whch extends RegisteredVehicle class.

-Read in the information from the vehicles.txt le and store the data in a single array of
objects

-Print out a report to the screen from the array of objects that has been created. It should look like this:

Make Model Wheels Weight Plate Date Registered Keeper Licence
----------- ----------- ------- ------- ------- -------- ---------------- ----------
Ford Focus 4 1200 XX12ABC 20100901 J Bloggs
Volkswagen Beetle 4 1310
Vauxhall Astra 4 1121 XY34WXZ 20030125 Aardvark's Taxis 0012567890
Reliant Robin 3 989 AB78DGH 20010329 D Trotter
Scania R420 Tipper 8 32500
Total: 5 vehicles

Broadly, I have started with classes. I am new to c++ and do not know how to read data from text file and output it to the screen by the format shown above. I would be very greatful if u help me to sort it out and show me the right pathway. P/S: The content of vehicle.txt is the same as the table above but without headings of columns.
What I have started:

#include <iostream>
using namespace std;
#ifndef VEHICLE_H
#define VEHICLE_H

//Create the class Vehicle

class Vehicle {
protected:
  char make[11];
  char model[21];
  int numWheels[3];
  float weight[8];
public:
  void initialize(int in_numWheels, float in_weight);
  int get_numWheels(void);
  float get_weight(void);
}


//Create the class RegisteredVehicle that extends Vehicle class

class RegisteredVehicle : public Vehicle()
{
	char numberPlate[8];
	int registrationDate[9];
	char registeredKeeper[21];
	int taxiLicence[11];
public:
	void initialize(int registrationDate, int taxiLicence);
	int get_registrationDate(void);
	int get_taxiLicence(void);
}


//Create the class RegisteredTaxi that extends RegisteredVehicle class

class  RegisteredTaxi : public RegisteredVegicle()
{
protected:
	int taxiLicence[10];
public:
	void initialize(int taxiLicence);
	int get_taxiLicence(void);
}

Recommended Answers

All 18 Replies

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>

using namespace std;

class Vehicle
{
    public:

     Vehicles();
     void get_line(ifstream&);
     void display_data();

     private:

     string make;
     string model;
     string plate;
     string registration_date;
     string keeper;
     string license;
     int wheels;
     double weight;
     bool is_taxi;
};

int main()
{
     Vehicle temp;

     vector<Vehicle> vehicles;

     ifstream infile("data.txt");
 
     //Provide simple error checking
     if(!infile.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(ifstream)
     {
          //Load the .txt file 'line-at-a-time'
          temp.get_line(infile);
          vehicles.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=vehicles.size(); i<size; i++)
     {
          vehicles[i].display_data();
     }

     return 0;
}

well written object oriented approach to doing your assignent. the program will attempt to open a file, load the file into a vector, and display the information to the user. all you have to do is write a few simple function definitions.

Line #46 should be

while(infile)

Thank you mate, it did help a lot! But the compiler gives me an error:
In function ‘int main()’:
VehicleClass.cpp:36: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::open()’
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/fstream:495: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]

I know it is a stupid question but could you point me where should i write function definitions?

Thank you mate

The member function you're trying to use is called is_open, not open.

@Narue

I changed it to is_open it gives the following:

#include<iostream>
  #include<cstdlib>
  #include<string>
  #include<vector>
  #include <fstream>
      using namespace std;
      class Vehicle
      {
      
public:
      void Vehicles();
      void get_line(ifstream&);
	void display_data();
private:
      char make[11];
      char model[21];
      char numberPlate[8];
      int registration_date[9];
      char registeredKeeper[20];
      int taxiLicence[10];
      int numWheels[9];
      double weight[9];
      bool is_taxi;
      };
      
      
  int main()
      {
      Vehicle temp;
      vector<Vehicle> vehicles;
      ifstream infile("vehicles.txt");


      //Provide simple error checking
      if(!infile.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);
      vehicles.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=vehicles.size(); i<size; i++)
      {
      vehicles[i].display_data();
      }
      return 0;
      }

Terminal:
In function ‘int main()’:
VehicleClass.cpp:36: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::open()’
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/fstream:495: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]

Do you know what is wrong here? Thanks mate

I changed it to is_open

No, you didn't. Line 35.

@Narue

Sorry mate) I have changed it now and it solved it, but gives an error:

In function `main':
VehicleClass.cpp:(.text+0x12a): undefined reference to `Vehicle::get_line(std::basic_ifstream<char, std::char_traits<char> >&)'
VehicleClass.cpp:(.text+0x19a): undefined reference to `Vehicle::display_data()'
collect2: ld returned 1 exit status

Have you implemented those two member functions?

No, i havent, my problem is i do not know how to do it :( should I put inside of display_data function like : display_data (char make, char model, int numWheel and so on?

So are you just typing characters randomly, or do you have a book on C++ that explains these fundamentals? I mean, defining a member function isn't exactly an obscure feature, it's among the most basic of basics when it comes to writing a class.

@Narue

No, i dont have a C++ book, i know it is a basic staff)), but how to relate function declaration to function definition in case of inputting a text file like in my case? should I use pointers? could you explain briefly this to me, please?

There are two ways:

// Inline in the class definition
class foo {
public:
    // This provides both a declaration and a definition
    void member_function()
    {
        // Implementation of member_function
    }
};
// Separate from the class definition
class foo {
public:
    // This provides a declaration only
    void member_function();
};

// This provides a definition
void foo::member_function()
{
    // Implementation of member_function
}
//in pseudocode

void Vehicle::display_data()
{
     //print 'make' to screen
     //print white spaces
     //print 'model' to screen
     //print white spaces
     //print 'wheels' to screen
     //print white spaces
     //print 'weight' to screen
     //print white spaces
     //print 'plate' to screen
     //print white spaces
     //print 'registration_date' to screen
     //print white spaces
     //print 'keeper' to screen
     //print white spaces
     //print 'license' to screen
     //print new line to screen
}

i don't know how i can make this any easier for you without just giving you the answer.

Seems like I am confused, and do not know enough in c++. I know it is impudence, but could you give me the answer please? Thank you mate, anyway. You did help me a lot.

We've done over 90% of the work for you... all we are asking for is a 10% effort on your part. I cannot ethically continue to work on this assignment without knowing that you will be turning in plagarized work.

Take it one step at a time. Do a little research on your own. Humor us... at least take a wild guess at how you would write the display() function.

Anything... I mean ANYTHING is better than "do it for me."

btw line #12 in the class should be

Vehicle();

my mistake.

I will try my best, and thank you very muchh!

@Clinton Portis

Sorry for bothering, but could you write pseudocode for Vehicle and get_line functions? am stuck

The following pseudocode is based on the assumption that each line will hold exactly 8 pieces of data:

//in pseudocode

void Vechicle::get_line(ifstream& infile)
{
     //read in the first word using the >> extraction operator and assign to 'make'
     //read in the second word using the >> extraction operator and assign to 'model'
     //read in the third word using the >> extraction operator and assign to 'wheels'
     //read in the fourth word using the >> extraction operator and assign to 'weight'
     //read in the fifth word using the >> extraction operator and assign to 'plate'
     //read in the sixth word using the >> extraction operator and assign to 'registration_date'
     //read in the seventh word using the >> extraction operator and assign to 'keeper'
     //read in the eighth word using the >> extraction operator and assign to 'license'
}

all i ask is that you do this work on your own (i hope you are not going to others asking them how to translate this pseudo in efforts to get your homework done for example) which may be the case since you don't have a book and this is probably the only way you will get your homework done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.