#include #include // for ifstream #include using namespace std; #include "tvector.h" #include "prompt.h" // file: context.cpp 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(tvector & text, int & size); void Display(const tvector & text, int size, string request); int Find(const tvector & text, int size, string request); int main() { int size; string request; tvector 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 tvector & 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 tvector & 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(tvector & 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.c_str()); size = 0; while (input >> word) { text[size] = word; size++; } cout << size << "words read." << endl; } /* Sample output: prompt> context Enter name of input file: context.cpp 408words read. search word: string string requested 75 location either side const string SENTINEL = "quit"; search word: main main requested 355 location "quit"; search word: main main requested 359 search word: main() main() requested 113 location string request); int main() { int size; search word: #include #include requested 0 location Illegal vector index: -3 max index = 499 /usr/project/courses/lib/tvector.cpp:169: failed assertion `0 <= k && k < mySize' Abort */