Reading from a text file to class objects

Pages: 12
Jib thank you for the information on using a vector intead of an array.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class trucks {
public :
    int ID;
    string Type;
    double feeder_pt;
    double axle_pt;
public:
    void setID (int id) {ID = id;}
    void setType (string type) {Type = type;}
    void setfeeder (double feeder) {feeder_pt = feeder;}
    void setaxle (double axle) {axle_pt = axle;}
};

int main ()
{
    ifstream myfile("sequence.txt");
    int ID;
    string Type;
    double feeder_pt;
    double axle_pt;


    trucks truck[5];
    for(int i = 0; i < 5; i++)
    if(myfile >> ID >> Type >> feeder_pt >> axle_pt)
     {
         truck[i].setID(ID);
         truck[i].setType(Type);
         truck[i].setfeeder(feeder_pt);
         truck[i].setaxle(axle_pt);


     }
       
}


Where do I actually define a vector, and do I need to change something else in the code?
> Where do I actually define a vector?

To declare a vector :
1
2
3
4
trucks truck[5];
for(int i = 0; i < 5; i++)
if(myfile >> ID >> Type >> feeder_pt >> axle_pt)
 {


==>
1
2
3
4
5
6
#include<vector>
vector<trucks> truck;
for(int i = 0; i < 5; i++)
if(myfile >> ID >> Type >> feeder_pt >> axle_pt)
 {
     truck.push_back(trucks());
Last edited on
(Does that help?) :)
closed account (48T7M4Gy)
Use vectors if you want to and there are many reasons why that is the best way to proceed. But before abandoning arrays altogether there is a very simple way to allocate enough space in the array and that is to read the file and count how many records there are. This means the file is read twice - no major problem.

(Adding and deleting records can be handled in a similar way without writing special array sizing functions that vectors have inbuilt.)
5a8Ym39o6 thank you again. How can I cout the elements of the vector to make sure that the values are stored in the objects? Also, is there is a way to make the loop stop until the end of the text instead of using 5?


Kemort thank you for your input. I guess a vector will serve my purpose more than an array because its size will be dependent on the elements I include in the text file.
closed account (48T7M4Gy)
Kemort thank you for your input. I guess a vector will serve my purpose more than an array because its size will be dependent on the elements I include in the text file.


Angel, I won't belabor the point beyond this comment but make sure you are clear that the number of elements in the text file here will invariably determine the size of the container whatever choice is made. There is no magic involved.

Go forth and vectorize! :)
Why are all the class member variables public? Normally the member variables should be private and then you would use member functions to access these private variables. In this instance you could use a constructor with the proper arguments to properly initialize the member variables, or you could use a temporary instance of your class and use your current functions to modify the member variables before inserting the temporary instance into the vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <string>
#include <fstream>
#include <vector>

using namespace std;

class truck
{
    private :
        int ID;
        string Type;
        double feeder_pt;
        double axle_pt;
    public:
        truck() {};
        truck(int id, string type, double feeder, double axle) :
            ID(id), Type(type), feeder_pt(feeder), axle_pt(axle) {}
        void print() const { cout << ID << " " << Type << " " << feeder_pt << " " << axle_pt << endl;}
};

int main()
{
    ifstream myfile("sequence.txt");
    int ID;
    string Type;
    double feeder_pt;
    double axle_pt;


    std::vector<truck> trucks;

    // Read the file, and insert into the vector.
    while(myfile >> ID >> Type >> feeder_pt >> axle_pt)
    {
        trucks.push_back({ID, Type, feeder_pt, axle_pt});
    }

    // Print the contents of the vector.
    for(auto& itr : trucks)
    {
        itr.print();
    }

}
Thank you again Jib.

I tried to run the code but I received three errors. The first one on this line
void print() const { cout << ID << " " << Type << " " << feeder_pt << " " << axle_pt << endl;}


What I had in my code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <string>
#include <fstream>
#include <vector>

using namespace std;

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class trucks {
public :
    int ID;
    string Type;
    double feeder_pt;
    double axle_pt;
public:
    void setID (int id) {ID = id;}
    void setType (string type) {Type = type;}
    void setfeeder (double feeder) {feeder_pt = feeder;}
    void setaxle (double axle) {axle_pt = axle;}
};

int main ()
{
    ifstream myfile("sequence.txt");
    int ID;
    string Type;
    double feeder_pt;
    double axle_pt;


vector<trucks> truck;
for(int i = 0; i < 5; i++)
if(myfile >> ID >> Type >> feeder_pt >> axle_pt)
 {
     truck.push_back(trucks());


 }
}


I guess it should be working, however, I cannot guarantee until I access the elements of the vector to make sure. This is what I don't know how to do..

Thank you
Try this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int i = 0;
vector<trucks> truck;
while(myfile >> ID >> Type >> feeder_pt >> axle_pt)
 {
     truck.push_back(trucks());
     truck[i].setID(ID);
     truck[i].setType(Type);
     truck[i].setfeeder(feeder_pt);
     truck[i].setaxle(axle_pt);
     i++;
 }

cout << "Print out all contents : " << endl;
for(i = 0; i < truck.size(); i++)
{
    cout << truck[i].ID << " ";
    cout << truck[i].Type << " ";
    cout << truck[i].feeder_pt << " ";
    cout << truck[i].axle_pt << endl;
}
Last edited on
(Does that help?) :)
Thank you 5a8Ym39o6.
The code is full of errors. It says that I did not declare the variables..
I don't know what the reason of the error honestly.
@Angelf92
> The code is full of errors.
Why do you say it is full of errors?
The following code works fine for me :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;
class trucks {
public :
    int ID;
    string Type;
    double feeder_pt;
    double axle_pt;
public:
    void setID (int id) {ID = id;}
    void setType (string type) {Type = type;}
    void setfeeder (double feeder) {feeder_pt = feeder;}
    void setaxle (double axle) {axle_pt = axle;}
};
int main ()
{
    ifstream myfile("sequence.txt");
    int ID;
    string Type;
    double feeder_pt;
    double axle_pt;

int i = 0;
vector<trucks> truck;
while(myfile >> ID >> Type >> feeder_pt >> axle_pt)
 {
     truck.push_back(trucks());
     truck[i].setID(ID);
     truck[i].setType(Type);
     truck[i].setfeeder(feeder_pt);
     truck[i].setaxle(axle_pt);
     i++;
 }
cout << "Print out all contents : " << endl;
for(i = 0; i < truck.size(); i++)
{
    cout << truck[i].ID << " ";
    cout << truck[i].Type << " ";
    cout << truck[i].feeder_pt << " ";
    cout << truck[i].axle_pt << endl;
}
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

class Truck {
private:
    int ID;
    std::string type;
    double feeder_pt;
    double axle_pt;
    
public:
    Truck(int, std::string, double, double);
    
    void setID (int);
    void setType (std::string);
    void setFeeder (double);
    void setAxle (double);
    
    int getID();
    std::string getType();
    double getFeeder ();
    double getAxle();
    
    std::string toString();
};

Truck::Truck(int aID, std::string aType, double a_Feeder_pt, double a_axle_pt)
{
    ID = aID;
    type = aType;
    feeder_pt = a_Feeder_pt;
    axle_pt = a_axle_pt;
}

void Truck::setID(int aID) { ID = aID; }
void Truck::setType (std::string aType) { type = aType; }
void Truck::setFeeder (double a_feeder_pt){ feeder_pt = a_feeder_pt; }
void Truck::setAxle (double a_axle_pt) { axle_pt = a_axle_pt; }

int Truck::getID() { return ID; }
std::string Truck::getType() { return type; }
double Truck::getFeeder () { return feeder_pt; }
double Truck::getAxle () { return axle_pt; }

std::string Truck::toString()
{ return std::to_string(ID) + ' ' + type + ' ' + std::to_string(feeder_pt) + ' ' + std::to_string(axle_pt);}

int main()
{
    int ID = 0;
    std::string type = "?";
    double feeder = 0;
    double axle = 0;
    int count = 0;
    
    Truck temp(0,"0",0,0);
    std::vector<Truck> vector_trucks;
    
    std::ifstream source("trucks.txt");
    
    if (source.is_open())
    {
        while (source >> ID >> type >> feeder >> axle)
        {
            temp.setID(ID);
            temp.setType(type);
            temp.setFeeder(feeder);
            temp.setAxle(axle);
            
            vector_trucks.push_back(temp);
        }
    }
    else
        std::cout << "Source file failed to open.\n";
    
    for(auto rit = vector_trucks.begin(); rit < vector_trucks.end(); rit++)
    {
        count++;
        std::cout << "Truck no " << count << ": " << (*rit).toString() << '\n';
    }
    
    source.close();
    
    return 0;
}



Truck no 1: 984763 A 20.000000 18.000000
Truck no 2: 762613 A 19.000000 17.000000
Truck no 3: 587123 A 22.000000 16.000000
Truck no 4: 897654 D 85.000000 19.000000
Program ended with exit code: 0
I tried to run the code but I received three errors. The first one on this line

You know it would help if you would tell use the errors you received, exactly as they appear in your development environment.

closed account (48T7M4Gy)
closed a/c + alphanumerals runs perfectly - oops code that is :)
Last edited on
Topic archived. No new replies allowed.
Pages: 12