#include #include #include using namespace std; int read_file(string ss[], string ns[], double as[]) { ifstream in; in.open("elements.txt"); if (in.fail()) { cout << "Could not open the file\n"; exit(1); } int maxatno; for (int atno = 1; atno < 150; atno += 1) { in >> ss[atno] >> ns[atno] >> as[atno]; if (in.fail()) break; maxatno = atno; } in.close(); return maxatno; } int find_element(string wanted, string ss[], int max) { for (int i = 1; i <= max; i += 1) if (ss[i] == wanted) return i; return -1; } int main() { string symbols[150], names[150]; double atwts[150]; const int maxatno = read_file(symbols, names, atwts); double total = 0.0; bool ok = true; while (true) { string wanted; cin >> wanted; if (wanted == ".") break; int position = find_element(wanted, symbols, maxatno); if (position >= 0) { cout << "Symbol: " << symbols[position] << ", Name: " << names[position] << ", Atwt = " << atwts[position] << "\n"; total += atwts[position]; } else { cout << "Couldn't find \"" << wanted << "\"\n"; ok = false; } } if (ok) cout << "\nMolecular weight = " << total << "\n"; }