From 5b9a183eea9c0ad576d6f1f693f6cbd70f212324 Mon Sep 17 00:00:00 2001 From: Dudeman Date: Mon, 4 May 2015 09:25:08 -0700 Subject: [PATCH] Update hw_getfile.cpp In your while-loop, you need to check that `pos` is not -1. -1 means that the substring (`word` in this case) was not found on that line. ANY positive integer means that `word` was found and thus is a match. So you only need to test `pos` against the value -1. I also put in a check for the number of arguments given. If somebody runs your program without arguments, you should give a helpful error message and exit non-zero. Without that check, your program was simply illegally accessing the array `argv` and being killed by the system for an "out of bounds" exception. A quick check against `argc` makes this common situation much prettier. --- hw_getfile.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/hw_getfile.cpp b/hw_getfile.cpp index 108f669..46a6274 100644 --- a/hw_getfile.cpp +++ b/hw_getfile.cpp @@ -9,7 +9,10 @@ using namespace std; //~~~~~~~~~~~~~~~~~~~~prints line from target word on. int main(int argc, char* argv[]){ - //for (int i = 0; i