Help with loading a struct with data from a file...

Hello, forum goers. I'm currently trying to learn how to load up a struct array with data from a file. Unfortunately it seems that I have no idea how to do so. I tried loading it in the same way I would load a 2D array, but that failed ... So any tips on where to go from here? This is the code that I currently have:

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
#include <iostream>
#include <string>	// needed for strings
#include <fstream> // needead for file IO
using namespace std;

// Define a struct of type Balls
struct Balls
{
	double diameter;
	string color;
};

int main()
{
	// create an object to handle the file input
	ifstream inputFile;

	// Opens the file
	inputFile.open("c:\\data.txt");

	// Checks if file opening was sucessful or not
	if (inputFile.fail())
	{
		cout << "The file does not exist!" << endl;
		exit(1);
	}

	// Initialize constants for the struct array (based on the data file)
	const int ROW = 4;
	const int COLUMN = 2;
	// Declare a struct array of type Ball with 4 Balls struct elements
	Balls ballCollection[ROW][COLUMN];

	// Initialize the ballCollection struct array
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COLUMN; j++)
                {
                   // write the data from file to struct
                    inputFile >> ballCollection[i][j];
                }
	}

	// Close the file 
	inputFile.close();

	// Display the items in the ball array as defined by the struct
	for (int i = 0; i < ROW; i++)
	{	                               						
		cout << "Diameter of ball: " << ballCollection[i].diameter << " and the Color: " << ballCollection[i].color << endl;
	}

	return 0;
}
1
2
// Declare a struct array of type Ball with 4 Balls struct elements
	Balls ballCollection[ROW][COLUMN];


This declares a 2D array containing a total of 8 balls

ballCollection[0][0]
ballCollection[1][0]
ballCollection[2][0]
ballCollection[3][0]

ballCollection[0][1]
ballCollection[1][1]
ballCollection[2][1]
ballCollection[3][1]

each of which is a Ball (with its own diameter and height)

1
2
// write the data from file to struct
                    inputFile >> ballCollection[i][j];

I don't know what your input file looks like so I can't tell you what will happen here.

ballCollection[i].diameter
ballCollection[i] is and array of 2 Balls. You can't get the diameter of an array.
Okay, so, declaring the struct array like that was a pretty bad idea then. It's supposed to be 4 balls. So then each element in this specific struct array counts as only 1 element of 2 types (which would be because that's how I defined it, right?). I guess that's what makes it a structure. Okay that makes a lot more sense.

By the way, the data file looks a bit like this:
1
2
3
4
12 orange
15.5 yellow
13 orange
52 yellow 


Some updated 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>	// needed for strings
#include <fstream> // needead for file IO
using namespace std;

// Define a struct of type Balls
struct Balls
{
	double diameter;
	string color;
};

int main()
{
	// create an object to handle the file input
	ifstream inputFile;

	// Opens the file
	inputFile.open("c:\\data.txt");

	// Checks if file opening was sucessful or not
	if (inputFile.fail())
	{
		cout << "The file does not exist!" << endl;
		exit(1);
	}

	// Initialize constants for the struct array (based on the data file)
	const int SIZE = 4;

	// Declare a struct array of type Ball with 4 Balls struct elements
	Balls ballCollection[SIZE];

	// Initialize the ballCollection struct array
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COLUMN; j++)
                {
                   // write the data from file to struct
                    inputFile >> ballCollection[i][j];
                }
	}

	// Close the file 
	inputFile.close();

	// Display the items in the ball array as defined by the struct
	for (int i = 0; i < SIZE; i++)
	{	                               						
		cout << "Diameter of ball: " << ballCollection[i].diameter << " and the Color: " << ballCollection[i].color << endl;
	}

	return 0;
}


EDIT: I am an idiot and got it to work now, thanks for the help! It made me realize a couple of things about structs that I had not though of before.
Last edited on
Topic archived. No new replies allowed.