#include #include #include using namespace std; class string_array { protected: string * data; int size; public: void init(int n) { data = new string[n]; size = n; } void enlarge() { int newsize = size * 2; string * newarray = new string[newsize]; for (int i = 0; i < size; i += 1) newarray[i] = data[i]; delete [] data; data = newarray; size = newsize; } void set(int index, string v) { if (index >= size) enlarge(); if (index >= 0) data[index] = v; else { cout << "ERROR in index\n"; exit(1); } } string get(int index) { if (index >= 0 && index < size) return data[index]; else { cout << "ERROR in index\n"; exit(1); } } int get_size() { return size; } }; int main() { string_array A; A.init(1); ifstream infile("/home/www/text/barrie/peterpan"); if (infile.fail()) { cout << "Couldn't open file\n"; exit(1); } int i = 0; while (true) { string s; infile >> s; if (infile.fail()) break; A.set(i, s); i += 1; } cout << "There were " << i << " words in the file\n"; while (true) { cout << "Enter a word: "; string s; cin >> s; for (int i = 0; i < A.get_size(); i += 1) { if (A.get(i) == s) { cout << "Found at position " << i << "\n"; break; } } } }