#include #include #include using namespace std; int main() { ifstream fin; fin.open("table.txt"); if (fin.fail()) { cout << "Couldn't open 'table.txt'\n"; exit(1); } // exit stops program from anywhere double total = 0.0; while (true) { string wanted; cout << "Enter symbol: "; cin >> wanted; if (wanted == "end") break; int atno = 0; fin.seekg(0, ios::beg); // reposition to 0 bytes from beginning while (atno < 111) { string symbol, name; double atwt; fin >> symbol >> name >> atwt; if (fin.fail()) // not error, just end of file break; atno = atno + 1; if (symbol == wanted) { cout << atno << ": " << name << " (" << symbol << ") wt = " << atwt << endl; total = total + atwt; break; } } } fin.close(); cout << "Molecular weight = " << total << endl; }