data from file into array of struct

How do i take the data from the .txt file and enter it into the array so that I can use it in the loop below.

here is the data in the text file

1
2
3
4
5
6
7
8
 {1,0,7.4,39.5,5.33784}, 
{3, 1, 4.6, 27.9, 6.06522}, 
{5,2,2.2,12.5,5.68182}, 
{8, 0, 14.5, 86, 5.93103}, 
{11, 1, 8, 43.8, 5.475}, 
{16, 2, 5.9, 37.7, 6.38983}, 
{22, 0, 12.7, 72, 5.66929}, 
{24, 1, 10.5, 63.2, 6.01905} 


Here is what i have so far,

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
88
#include<iostream>
#include<fstream> 

using namespace std;
std::ifstream infile("fields.txt");

int initialise(int field, int crop, float size, float yof, float yph);

struct Fields {
	

	int Field;
	int Crop;
	float Size;
	float Yof;
	float Yph;

	int initialise(int field, int crop, float size, float yof, float yph)
	{
		Field = field;
		Crop = crop;
		Size = size;
		Yof = yof;
		Yph = yph;

	};

};



int main() {

	Fields fielddata[8];

	
	ifstream file("fields.txt");
	if(file.is_open())
	{
		

	
		int a, b, i = 0;
		float c, d, e;
		while (infile >> a >> b >> c >> d >> e)
		{
			fielddata[i].Field = a;
			fielddata[i].Crop = b;
			fielddata[i].Size = c;
			fielddata[i].Yof = d;
			fielddata[i].Yph = e;

			++i;
		}
	
	
	}




int highyph = 0;



	cout << "Field\t" << "Crop\t" << "Size\t" << "YOF\t" << "YPH\t" << endl;

	for (int i = 0; i < 8; i++) {


		cout << fielddata[i].Field << "\t" << fielddata[i%3].Crop << "\t" << fielddata[i].Size << "\t" << fielddata[i].Yof << "\t" << fielddata[i].Yph << "\t" << endl;
	}
	

	for (int i = 0; i < 8; i++)
	{
		if (fielddata[i].Yph > highyph)
			highyph = fielddata[i].Field;
	}

	cout << "The Field with the Highest Yield is " << highyph << endl;




	system("Pause");
		return 0;
}
Last edited on
When reading from the file, your code does not take into account the characters '{', ',' and '}' in your input file.
Could you tell me how to fix this?
If you have a choice, you could change the input file like this:
1 0 7.4 39.5 5.33784 
3 1 4.6 27.9 6.06522 
5 2 2.2 12.5 5.68182 
8 0 14.5 86 5.93103 
11 1 8 43.8 5.475 
16 2 5.9 37.7 6.38983 
22 0 12.7 72 5.66929 
24 1 10.5 63.2 6.01905


But if you need to continue with the existing file format, you might try something like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    int count = 0;    
    string line;
    while (getline(file, line, '}'))
    {
        istringstream ss(line);
        ss.ignore(100, '{');
        int a, b;
        float c, d, e;
        char ch;
        if (ss >> a >> ch >> b >> ch >> c >> ch >> d >> ch >> e)
        {
            // store in fielddata[count] here
            count++;
        }
    }


wow thanks very much
just one more question if you dont mind @Chervil

i've been trying to make this for loop work properly and display the field id with the highest yield per hectare, this is what i tried but it returns the wrong id. I've tried playing around with it but no luck


1
2
3
4
5
6
7
8

	for (int i = 0; i < 8; i++)
	{
		if (fielddata[i].Yph > highyph)
			highyph = fielddata[i].Field;
	}

	cout << "The Field with the Highest Yield is " << highyph << endl;
Your code is mixing two different variables. One is an int, the other a float. You need two separate variables
1
2
    float highYph   = 0; 
    int highField   = 0;
and update both of them when a higher value is found.

Or use an object of type Fields.
Last edited on
Topic archived. No new replies allowed.