How can I get ispunct to work in the program

  c++, csv, punctuation

I need help building this C++ program. I am currently trying to build a program that will read all the words inside of a .csv file and then display the word and the frequency of each word in another output .csv file. I am having problems, however, getting ispunct to erase all the punctuations within the text of the .csv file so that, for example, "here" and "here." are not counted as different strings. Can someone please help to explain to me what is wrong with my code below? Thank you.

#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
using namespace std;

string filename="keywords.csv";

typedef std::pair <string, int> myPair;
typedef std::vector <myPair> myVector;
myVector vec;

typedef std::map<string, int> myMap;

void countWords(istream& in, myMap& words) {
    string s;
    int i, len;
    while (in>>s)
    {
        for(i=0, len=s.size(); i<len; i++)
        {
            if(ispunct(s[i]))
            {
                s.erase(i--,1);
                len=s.size();
            }
            ++words[s];
        }
    }
}


int main () {

    ifstream jobs;
    jobs.open("jobs.csv");

    if (jobs.fail())
    {
        cout<<"The jobs file was not successfully opened."<<endl;
        exit (1);
    };

    myMap mappingWords;
    countWords(jobs, mappingWords);

    for (myMap::iterator it = mappingWords.begin(); it!=mappingWords.end(); ++it){
        cout<<it->first<<" occurred " <<it->second<< " times.n";
    }

    copy(mappingWords.begin(), mappingWords.end(), back_inserter<myVector>(vec));

    fstream outFile(filename, ios::out);
    outFile<<"Words"<<" , "<<"# of times repeated"<<endl;

    //Printing the vector in the output file.
    for (auto const &pair: vec)
    {
        outFile<<pair.first<<" , "<<pair.second<<'n';
    }

    //Clean-up
    vec.clear();
}

Source: Windows Questions C++

LEAVE A COMMENT