Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are examples like this posted all over the internet. Here is a count-words program I wrote back when I was in high school. Use it as a starting point. Other things I would like to point out are:</p> <p>std::stringstream :you std::getline the entire line, then use std::stringstream to chop it up into smaller pieces and tokenise it. You can get the entire line using std::getline and inputting it into a std::string, which you can then pass to std::stringstream.</p> <p>Once again, this is only an example and won't do exactly what you want it to do, you will need to modify it yourself to make it do what you want it to do!</p> <pre><code>#include &lt;iostream&gt; #include &lt;map&gt; #include &lt;string&gt; #include &lt;cmath&gt; #include &lt;fstream&gt; // Global variables std::map&lt;std::string, int&gt; wordcount; unsigned int numcount; void addEntry (std::string &amp;entry) { wordcount[entry]++; numcount++; return; } void returnCount () { double percentage = numcount * 0.01; percentage = floor(percentage + 0.5f); std::map&lt;std::string, int&gt;::iterator Iter; for (Iter = wordcount.begin(); Iter != wordcount.end(); ++Iter) { if ((*Iter).second &gt; percentage) { std::cout &lt;&lt; (*Iter).first &lt;&lt; " used " &lt;&lt; (*Iter).second &lt;&lt; " times" &lt;&lt; std::endl; } } } int main(int argc, char *argv[]) { if (argc != 2) { std::cerr &lt;&lt; "Please call the program like follows: \n\t" &lt;&lt; argv[0] &lt;&lt; " &lt;file name&gt;" &lt;&lt; std::endl; return 1; } std::string data; std::ifstream fileRead; fileRead.open(argv[1]); while (fileRead &gt;&gt; data) { addEntry(data); } std::cout &lt;&lt; "Total words in this file: " &lt;&lt; numcount &lt;&lt; std::endl; std::cout &lt;&lt; "Words that are 1% of the file: " &lt;&lt; std::endl; returnCount(); } </code></pre>
 

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