Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ problem in calling a function in main that prints the map
    text
    copied!<p>I am trying to print the contents of the map and this is where my code fails. I have tested all my methods and I have no problem to read from file, filer the word, put it into map, and even the print function is working. However, when I am calling the printer function from main it does not print the map. I am new to polymorphism and I think that my error is in how I am passing the map to the function in main. </p> <p>Here is my main class:</p> <pre><code>using namespace std; #include &lt;iostream&gt; #include "ReadWords.h" #include "ReadPunctWords.h" #include "ReadNumWords.h" #include "ReadCapWords.h" #include "MapWorks.h" #include &lt;fstream&gt; #include &lt;string&gt; #include &lt;map&gt; #include &lt;iterator&gt; /** * This main function uses all other classes. */ int main() { char* name = "RomeoJuliet.txt"; //ReadPunctWords &amp;obj = *new ReadPunctWords(name); ReadPunctWords obj(name); string startSearch="BEGIN"; string endSearch="FINIS"; ReadPunctWords rpw; ReadCapWords rcw; ReadNumWords rnw; MapWorks mw; while(rpw.isNextWord()){ string tempword = obj.getNextWord(); if(tempword == startSearch){ break; } } while(rpw.isNextWord()){ string tempword = obj.getNextWord(); if(tempword == endSearch){ break; } else{ if(rpw.filter(tempword)){ mw.addToMap(tempword, mw.mapPunct); } if(rcw.filter(tempword)){ mw.addToMap(tempword, mw.mapCap); } if(rnw.filter(tempword)){ mw.addToMap(tempword, mw.mapNum); } } } mw.printMap(mw.mapPunct); mw.printMap(mw.mapCap); mw.printMap(mw.mapNum); //clear map mw.clearMap(mw.mapPunct); mw.clearMap(mw.mapCap); mw.clearMap(mw.mapNum); //close the file //obj.close(); //delete &amp;obj; //exit(0); // normal exit return 0; } </code></pre> <p>And my MapWorks.cpp which contains the maps and the functions related to maps:</p> <pre><code>using namespace std; #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;map&gt; #include &lt;iterator&gt; #include "MapWorks.h" /** * MapWorks class builds the maps and does the map processing and printing */ MapWorks::MapWorks() {} void MapWorks::addToMap(string myword, map&lt;string, int&gt; &amp; myMap){ int n = myMap[myword]; myMap[myword]= n+1; } void MapWorks::printMap (map&lt;string, int&gt; &amp;myMap){ for (map&lt;string, int&gt;::iterator it = myMap.begin(); it != myMap.end(); ++it) { cout &lt;&lt; it-&gt;first &lt;&lt; " ==&gt; " &lt;&lt; it-&gt;second &lt;&lt; '\n'&lt;&lt;endl; } } //delete entries in map void MapWorks::clearMap(map&lt;string, int&gt;myMap) { myMap.clear(); } </code></pre> <p>MapWorks.h :</p> <pre><code>#ifndef MAPWORKS_H #define MAPWORKS_H #include &lt;string&gt; #include &lt;map&gt; using namespace std; /** * MapWorks class builds the maps and does the map processing and printing */ class MapWorks { public: map&lt;string, int&gt; mapPunct; //(word, number of occurences) map&lt;string, int&gt; mapNum; //(word, number of occurences) map&lt;string, int&gt; mapCap; //(word, number of occurences) MapWorks(); void addToMap(string myword, map&lt;string, int&gt; &amp; myMap); //adds words to a map void printMap (map&lt;string, int&gt; &amp;myMap); //prints the map void clearMap(map&lt;string, int&gt;); //clear map }; #endif </code></pre> <p>My ReadWords.h :</p> <pre><code>/** * ReadWords class, the base class for ReadNumWords, ReadPunctWords, ReadCapWords */ #ifndef READWORDS_H #define READWORDS_H using namespace std; #include &lt;string&gt; #include &lt;fstream&gt; #include&lt;iostream&gt; class ReadWords { private: string nextword; ifstream wordfile; bool eoffound; public: /** * Constructor. Opens the file with the default name "text.txt". * Program exits with an error message if the file does not exist. */ ReadWords(); /** * Constructor. Opens the file with the given filename. * Program exits with an error message if the file does not exist. * @param filename - a C string naming the file to read. */ ReadWords(char *filename); /** * Closes the file. */ void close(); /** * Returns a string, being the next word in the file. * @return - string - next word. */ string getNextWord(); /** * Returns true if there is a further word in the file, false if we have reached the * end of file. * @return - bool - !eof */ bool isNextWord(); //pure virtual function for filter virtual bool filter(string word)=0; /** * Fix the word by the definition of "word" * end of file. * @return - string */ string fix(string word); }; #endif </code></pre> <p>And my ReadPunctWords (ReadNumWords and ReadCapWords are quite the same, just checking if the word has digits or capital letters instead of punctuations like in here):</p> <pre><code>#ifndef READPUNCTWORDS_H #define READPUNCTWORDS_H using namespace std; #include &lt;string&gt; #include "ReadWords.h" /** * ReadPunctWords inherits ReadWords, so MUST define the function filter. * It chooses to override the default constructor. */ class ReadPunctWords: public ReadWords { public: ReadPunctWords(); ReadPunctWords(char *filename): ReadWords(filename){}; virtual bool filter(string word); }; #endif </code></pre> <p>I would appreciate any help from you. Thanks, Adriana</p>
 

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