Note that there are some explanatory texts on larger screens.

plurals
  1. POProper place in nests to put cout
    primarykey
    data
    text
    <p>EDIT: Changed to a boolean flag, but still prints more than it should: </p> <pre><code>string line; // a string to hold the current line while(getline(myFile,line)) { bool old_count = false; // to help determine whether the line has been output yet for (unsigned int i = 0; i &lt; line.length(); i++) { string test = line.substr( i, targ_length ); if ( strcmp(word.c_str(),test.c_str()) == 0 ) { count++; if ( !old_count ); { cout &lt;&lt; line_num &lt;&lt; " : " &lt;&lt; line &lt;&lt; endl; } // end if old_count=true; } // end if } //end for line_num++; } // end while </code></pre> <p>/end edit</p> <p>I have an assignment to write a program to search for a word in a text file. I've got it to work perfectly except, it is supposed to print each line that the word is found in, and my program will print the same line multiple times if the word appears multiple times in the line. I need it to only print a line once. I've tried moving the if (count != old_count) around different places but not luck and got confused. My code is below. Thanks for any help! </p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; #include &lt;cstring&gt; using namespace std; /* minimum required number of parameters */ #define MIN_REQUIRED 3 /* display usage */ int help() { printf("Proper usage: findWord &lt;word&gt; &lt;file&gt;\n"); printf("where\n"); printf(" &lt;word&gt; is a sequence of non-whitespace characters\n"); printf(" &lt;file&gt; is the file in which to search for the word\n"); printf("example: findWord the test.txt\n"); return 1; } /* * Program that searches for occurrences of given word within a given file * @return 0 (default for a main method) */ int main(int argc, char *argv[]) { if (argc &lt; MIN_REQUIRED) { return help(); } // end if string word = argv[1]; // the word to be searched for string file_name = argv[2]; // the name of the file to be read ifstream myFile(file_name.c_str()); // read the file if (! myFile) { cerr &lt;&lt; "File '" &lt;&lt; file_name &lt;&lt; "' could not be opened" &lt;&lt; endl; return -1; } // end if cout &lt;&lt; "Searching for '" &lt;&lt; word &lt;&lt; "' in file '" &lt;&lt; file_name &lt;&lt; "'\n"; int targ_length = word.length(); // the legnth of the string we're searching for int count = 0; // running count of instances of word found int line_num = 1; // number of current line string line; // a string to hold the current line while(getline(myFile,line)) { int old_count = count; // to help determine whether the line has been output yet for (unsigned int i = 0; i &lt; line.length(); i++) { string test = line.substr( i, targ_length ); if ( strcmp(word.c_str(),test.c_str()) == 0 ) { count++; } // end if if ( old_count != count ); { cout &lt;&lt; line_num &lt;&lt; " : " &lt;&lt; line &lt;&lt; endl; } // end if } //end for line_num++; } // end while cout &lt;&lt; "# occurrences of '" &lt;&lt; word &lt;&lt;" ' = " &lt;&lt; count &lt;&lt; endl; return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload