/* This shows only the ptable struct definition and main, not the whole program The things to look at are the protected:, public:, get_number, and get_element in ptable, and main itself. */ struct ptable { protected: element T[150]; int max, num; public: ptable() { max = 150; num = 0; T[0] = element(0, "Q", "Nosuchthingium", 0.0); } int get_number() { return num; } element get_element(int i) { if (i>=1 && i<=num) return T[i]; else return T[0]; } void read_table() { ifstream elfile("elements.txt"); if (elfile.fail()) { cerr << "could not open elements.txt\n"; return; } while (true) { string n, s; double a; elfile >> s >> n >> a; if (elfile.fail()) break; num = num + 1; if (num >= max) { cerr << "too many elements\n"; exit(1); } T[num] = element(num, s, n, a); } elfile.close(); cout << num << " elements read\n"; } element find_sym(const string & sy) { int i = 1; while (i <= num) { if (T[i].symbol == sy) return T[i]; i = i + 1; } return T[0]; } }; int main() { ptable PT; PT.read_table(); cout << "element 17 is " << PT.get_element(17) << "\n"; cout << "element 107 is " << PT.get_element(107) << "\n"; cout << "element 157 is " << PT.get_element(157) << "\n"; } /* Everything has been made safe Brainless users can't cause disasters with cout << "element 17 is " << PT.T[17] << "\n"; cout << "element 107 is " << PT.T[107] << "\n"; cout << "element 157 is " << PT.T[157] << "\n"; any more, because T is proected */