#include #include #include using namespace std; struct element { int atno; string symbol; string name; double atwt; }; int readfile(element A[], int max) { ifstream f("/home/www/class/een118/a3010b.txt"); if (f.fail()) { cout << "Can't read the data file\n"; exit(1); } int pos = 1; while (true) { if (pos >= max) { cout << "The array isn't big enough\n"; exit(1); } f >> A[pos].symbol >> A[pos].name >> A[pos].atwt; if (f.fail()) break; A[pos].atno = pos; pos += 1; } f.close(); A[0].atno = 0; A[0].symbol = "Error"; A[0].name = "Error"; A[0].atwt = 0.0; return pos - 1; } void printtable(element A[], int N) { for (int i = 1; i <= N; i += 1) cout << i << ": " << A[i].atno << " " << A[i].symbol << " " << A[i].name << " " << A[i].atwt << "\n"; } element find(string wanted, element A[], int num) { for (int i = 1; i <= num; i += 1) if (A[i].symbol == wanted) return A[i]; return A[0]; } int main() { const int arraysize = 200; element table[arraysize]; int number = 0; number = readfile(table, arraysize); while (true) { cout << "? "; string s; cin >> s; element r = find(s, table, number); if (r.atno == 0) cout << "Element " << s << " is unknown\n"; else cout << r.atno << " " << r.symbol << " " << r.name << " " << r.atwt << "\n"; } }