#include #include // for ifstream #include // for exit() #include // for tolower() #include // for CHAR_MAX #include "CPstring.h" #include "prompt.h" #include "vector.h" // file: context.cc DRAFT!! // read text, as words, in program // interactively, display requested word with C words before and after. // author Dietolf Ramm; draft date: 10/30/96 const int C = 3; // number of words to display on either side const string SENTINEL = "quit"; // word to cause exit from request loop void Load(Vector & text, int & size); void Display(const Vector & text, int size, string request); int Find(const Vector & text, int size, string request); int main() { int size; string request; Vector text(500); Load(text, size); cout << "search word: "; cin >> request; while(request != SENTINEL) { Display(text, size, request); cout << "search word: "; cin >> request; } return 0; } int Find(const Vector & text, int size, string request) // postcondition: k points to matching word in text or returns -1 { int k; for (k = 0; k < size; k++) if (text[k] == request) return k; return -1; } void Display(const Vector & text, int size, string request) // postcondition: word located and printed in context or msg if not found { int k, where; cout << request << " requested" << endl; where = Find(text, size, request); cout << where << " location" << endl; if (where >= 0) { for (k = where - C; k <= where + C; k++) cout << text[k] << " "; cout << endl; } else cout << " not found" << endl; } void Load(Vector & text, int & size) // postcondition: text contains words read in with size telling how many { string word; string filename = PromptString("Enter name of input file: "); ifstream input(filename); size = 0; while (input >> word) { text[size] = word; size++; } cout << size << "words read." << endl; } Sample output: context Enter name of input file: context.cc 456words read. search word: string string requested 70 location either side const string SENTINEL = "quit"; search word: main main requested -1 location not found search word: main() main() requested 108 location string request); int main() { int size; search word: #include #include requested 0 location Illegal vector index: -3 (max = 500) Assertion failed: index >= 0, file /usr/project/courses/cps008/lib/vector.h, line 191 Abort (core dumped)