Note that there are some explanatory texts on larger screens.

plurals
  1. POCleaning a string of punctuation in C++
    text
    copied!<p>Ok so before I even ask my question I want to make one thing clear. I am currently a student at NIU for Computer Science and this does relate to one of my assignments for a class there. So if anyone has a problem read no further and just go on about your business. </p> <p>Now for anyone who is willing to help heres the situation. For my current assignment we have to read a file that is just a block of text. For each word in the file we are to clear any punctuation in the word (ex : "can't" would end up as "can" and "that--to" would end up as "that" obviously with out the quotes, quotes were used just to specify what the example was).</p> <p>The problem I've run into is that I can clean the string fine and then insert it into the map that we are using but for some reason with the code I have written it is allowing an empty string to be inserted into the map. Now I've tried everything that I can come up with to stop this from happening and the only thing I've come up with is to use the erase method within the map structure itself.</p> <p>So what I am looking for is two things, any suggestions about how I could a) fix this with out simply just erasing it and b) any improvements that I could make on the code I already have written.</p> <p>Here are the functions I have written to read in from the file and then the one that cleans it. </p> <p>Note: the function that reads in from the file calls the clean_entry function to get rid of punctuation before anything is inserted into the map.</p> <p>Edit: Thank you Chris. Numbers are allowed :). If anyone has any improvements to the code I've written or any criticisms of something I did I'll listen. At school we really don't get feed back on the correct, proper, or most efficient way to do things.</p> <pre><code>int get_words(map&lt;string, int&gt;&amp; mapz) { int cnt = 0; //set out counter to zero map&lt;string, int&gt;::const_iterator mapzIter; ifstream input; //declare instream input.open( "prog2.d" ); //open instream assert( input ); //assure it is open string s; //temp strings to read into string not_s; input &gt;&gt; s; while(!input.eof()) //read in until EOF { not_s = ""; clean_entry(s, not_s); if((int)not_s.length() == 0) { input &gt;&gt; s; clean_entry(s, not_s); } mapz[not_s]++; //increment occurence input &gt;&gt;s; } input.close(); //close instream for(mapzIter = mapz.begin(); mapzIter != mapz.end(); mapzIter++) cnt = cnt + mapzIter-&gt;second; return cnt; //return number of words in instream } void clean_entry(const string&amp; non_clean, string&amp; clean) { int i, j, begin, end; for(i = 0; isalnum(non_clean[i]) == 0 &amp;&amp; non_clean[i] != '\0'; i++); begin = i; if(begin ==(int)non_clean.length()) return; for(j = begin; isalnum(non_clean[j]) != 0 &amp;&amp; non_clean[j] != '\0'; j++); end = j; clean = non_clean.substr(begin, (end-begin)); for(i = 0; i &lt; (int)clean.size(); i++) clean[i] = tolower(clean[i]); } </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