C++/c++

Advertisement


Question
i hv done the changes,nt sure is right or wrong.but wen i compile it display nthing.nt sure which prt of it is wrong.pls help,i realli hv no idea.appericate for all the help frm u.

code:

#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <map>
#include <ctype.h>
#include <set>


using namespace std;


void removePunct(string&);
void lowerCase(string&);


int main(int argc, char *argv[])
{
   map<string,int> freq;
   map<string,int>::iterator i;
  set<string> ignore;    // Words to ignore.
  
  string file,line,word;
   stringstream ss;

   cout << "Text file name: ";
   getline(cin,file);
   //-- Read file of words to ignore.
   ifstream myFile("common.txt");
   while (myFile >> word)
   {
       ignore.insert(word);
   }

   //-- Read words
   while (cin >> word)
  {
       if (ignore.find(word) == ignore.end())
     {
         freq.erase(word); //erase
       }
   }
  
   ifstream myfile(file.c_str());
   
   if (myfile.is_open())
  {
      while (getline(myfile,line))
      {
        ss.str(line);

      while (ss.good())
     {
         if (ss >> word)
         {
         removePunct(word);
         if (!word.empty())
         {
         lowerCase(word);
         if ((i = freq.find(word)) == freq.end())
         {
         freq.insert(pair<string,int>(word,1));
         } else
         {
         freq[word]++;
         }
         }
         }

       }
       ss.clear();

   }
   }else
    {
       cout << "Unable to open file" << file << endl;
     }
       
       cout << "Total unique words: " << freq.size() << endl;
      cout << endl;
     
   for (i = freq.begin();i != freq.end(); ++i)
  {
    cout << setw(14) << i->first;
    cout << setw(10) << i->second << endl;
   }
    
    cout << endl;
    
   system("PAUSE");
   return(0);
}

  

void removePunct(string &s)
{
   string::iterator i = s.begin();
   while (i != s.end())
  {
       if ((ispunct(*i)) || (isdigit(*i)))
       {
         s.erase(i);
     }
       else
     {  
         ++i;
     
     }
  }
}


void lowerCase(string& s)
{
  string::iterator i = s.begin();
  while (i != s.end())
  {
      *i = tolower(*i);
      ++i;
  }
}  

Answer
Hello Annabelle

Look closely at the code and think about what the program is doing here

   //-- Read file of words to ignore.
   ifstream myFile("common.txt");
   while (myFile >> word)
   {
       ignore.insert(word);
   }

   //-- Read words
   while (cin >> word)
   {
       if (ignore.find(word) == ignore.end())
       {
         freq.erase(word); //erase
       }
   }
  
   ifstream myfile(file.c_str());

The program is reading in the ignore list. That's good. Then its removing words from freq if the word is NOT on the ignore list. This is wrong for two reasons. First, the program has not even put anything on freq yet. Do you see that you are calling freq.erase before anything was put on freq? Second, if you were erasing from freq, you should do so if the word IS found on the ignore list. In other words, erase only
if(ignore.find(word) != ignore.end()).

I don't know why you suddenly decided to read words from cin.

You have two choices.
Choice 1) RECOMMENDED
In the loop while(ss.good()), after changing your word to lowercase, check if the word is in the common words set, and if it is, don't put it on the map. In other words, put a word on freq only if
(ignore.find(word) == ignore.end())

Choice 2) NOT RECOMMENDED
After processing the input file, go through each word in the common set, and erase the word from the map if it is contained on the map. If you choose this method, it is important to iterate through the set, and delete from the map. You cannot iterate over the map, because deleting from the map will make the iterator invalid. That point is subtle, and is the reason I don't recommend this approach.


Remember to convert the common words to lowercase before putting them on the set.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.