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:
#include <iostream>
#include <string> // needed for strings
#include <fstream> // needead for file IO
usingnamespace 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)
constint ROW = 4;
constint 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;
}
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.
#include <iostream>
#include <string> // needed for strings
#include <fstream> // needead for file IO
usingnamespace 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)
constint 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.