data transfer from text file to a single array
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:
C++ Syntax (Toggle Plain Text)
1.
2.
3.
#include <iostream>
4.
using namespace std;
5.
#ifndef VEHICLE_H
6.
#define VEHICLE_H
7.
8.
//Create the class Vehicle
9.
10.
class Vehicle {
11.
protected:
12.
char make[11];
13.
char model[21];
14.
int numWheels[3];
15.
float weight[8];
16.
public:
17.
void initialize(int in_numWheels, float in_weight);
18.
int get_numWheels(void);
19.
float get_weight(void);
20.
}
21.
22.
23.
//Create the class RegisteredVehicle that extends Vehicle class
24.
25.
class RegisteredVehicle : public Vehicle()
26.
{
27.
char numberPlate[8];
28.
int registrationDate[9];
29.
char registeredKeeper[21];
30.
int taxiLicence[11];
31.
public:
32.
void initialize(int registrationDate, int taxiLicence);
33.
int get_registrationDate(void);
34.
int get_taxiLicence(void);
35.
}
36.
37.
38.
//Create the class RegisteredTaxi that extends RegisteredVehicle class
39.
40.
class RegisteredTaxi : public RegisteredVegicle()
41.
{
42.
protected:
43.
int taxiLicence[10];
44.
public:
45.
void initialize(int taxiLicence);
46.
int get_taxiLicence(void);
47.
}
Re: data transfer from text file to a single array
Quote:
Originally Posted by
Marc G
If the license is just a number, you can simply store it in "int taxiLicense" if license number is below the number around 2 billion.
Unless it has leading zeros.
Re: data transfer from text file to a single array
First, please use [ code ] [ /code ] tags when posting code with proper indentation.
Second, I would recommend you to remove all fixed character arrays like "char numberPlate[8]" and replace them with "std::string numberPlate".
Also, why are you using integer arrays? What is "int taxiLicence[11]" suppose to represent? If the license is just a number, you can simply store it in "int taxiLicense" if license number is below the number around 2 billion.
Instead of:
Code:
class RegisteredVehicle : public Vehicle()
you should do:
Code:
class RegisteredVehicle : public Vehicle
Instead of using initialize functions, you should do that in the constructor of your class.
Based on the above comments, I'm thinking you really should grab an introductory book on C++ and read it as it seems you are missing some fundamental understanding of C++.
Re: data transfer from text file to a single array
Quote:
Originally Posted by
GCDEF
Unless it has leading zeros.
Ah, right, wasn't thinking about leading zeros. But in that case, it's probably easier to work with them by just storing them as strings instead.
Ps: something seems strange with times on the codeguru server. Your reply came earlier than my post :confused:
Re: data transfer from text file to a single array
Quote:
Originally Posted by
Marc G
Ps: something seems strange with times on the codeguru server. Your reply came earlier than my post :confused:
I noticed. I've seen it do that before on occasion.
Re: data transfer from text file to a single array
thanks you guys a lot, sorry for not posting the code in the right format. yeah, i dont have a good fundament in c++ as i am new to it)) So far I came up the following code to this problem, but have some compiler errors:
Code:
#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[21];
int taxiLicence[11];
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.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);
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;
}
Code:
Terminal:
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
I need to implement member functions, but i dont know what should I write in it. Would be thankful if you could help me and direct me how to do it.
Re: data transfer from text file to a single array
Quote:
Originally Posted by
Salamu
thanks you guys a lot, sorry for not posting the code in the right format. yeah,
You also posted in the wrong forum.
First, none of your code is specific to Visual C++. Second, you aren't even using Visual C++, as noted by the linker you're using. You are using "ld" to link, and that is something from the gcc world.
Next time, please post in the non-Visual C++ forum.
As to your error, where are the get_line() and display_data functions implemented? The linker is looking for those functions, and can't find them. You're calling them, but where are they found? And no, just declaring it in your class is not implementing the function.
Regards,
Paul McKenzie
Re: data transfer from text file to a single array
sorry, i meant "not in the right format" :) thanks