// Bjarne Stroustrup 1/17/2010 // Chapter 10 Exercise 12 /* Write a program that given a file name and a word outputs each line that contains that word together with the line number. Hint: getline(). */ #include "../std_lib_facilities.h" /* What is "a word"? I "carelessly" used that phrase in the problem statement and now I/we have to decide what it should mean. Beware of casual wording in requirements. OK: Let's define "word" as "whatever string's >> think it is (that is, "a whitespace terminated sequence of characters"). Alternative definitions, such as "an arbitrary sequence of characters specified by the user" could be even more interesting as an exercise. This exercise gets much easier after Chapter 11! (then I could use getline() and strstream). However, let me solve it here without using anything from Chapter 11 except the getline() mentioned in the hint. */ bool match(const string& s, int i, const string& w) // is s[i] the start of w? { for (int j=0; j>name; ifstream ifs(name.c_str()); if (!ifs) error("bad input file name: ", name); cout << "Please enter a word to search for: "; string word; cin >> word; int line_no = 0; string line; while (getline(ifs,line)) { ++line_no; // we read another line for (int i=0; i