#include #include #include // on other systems you would need to put // using namespace std; // here. I eliminated it on rabbit. struct element { string name, symbol; double weight; }; element table[105]; // setting the array size to the "exactly right" 105 is not // good design. One day the data file may be bigger, and // the program would just stop working properly. element make(string s, string n, double w) { element r; r.symbol=s; r.name=n; r.weight=w; return r; } void print(element e) { cout << e.name << " (" << e.symbol << ") " << "at.wt.=" << e.weight << "\n"; } void read() { ifstream efile("elements.txt"); if (efile.fail()) { cerr << "Can't open 'elements.txt'\n"; // remember why it's cerr not cout. exit(1); } for (int i=1; i<=104; i+=1) // again, the exact 104 is not great design. { string sy, nm; double wt; efile >> sy >> nm >> wt; if (efile.fail()) // remember why fail() must be tested right here. break; table[i]=make(sy, nm, wt); } efile.close(); } void test() { print(table[11]); print(table[57]); print(table[103]); } void main() { read(); test(); }