Maps
Sep 22, 2008 at 7:41pm UTC
I am writing a program that reads in from a txt file using cin NOT fstream, assignment guidelines. It takes the words and separates any punctuation marks. The clean_entry function is already written. I am having a hard time figuring out how to implement it into the get_word function. Could someone explain how to do this.
This is what i have so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#include <cstdlib>
#include <iostream>
#include <string>
#include <map>
#include <iomanip>
#include <fstream>
using namespace std;
typedef map<string, int > mapType;
void clean_entry(const string& source, string& target)
{
int p1 = 0, p2;
while (!isalnum(source[p1]) && p1 < source.length()) p1++;
p2 = p1;
while (isalnum(source[p2])&& p2 < source.length()) p2++;
if ( p2 == p1 ) target = "" ;
else target = source.substr(p1, p2-p1);
return ;
}
void print_words(const map<string, int >& wordcounts, int nemptyw)
{
int i = 0;
cout << "Nonemmpty words : " << nemptyw << endl;
cout << "Distinct words : " << wordcounts.size() << endl;
for (mapType::const_iterator it = wordcounts.begin(); it != wordcounts.end(); ++it)
{
cout << setw (15);
cout << setiosflags (ios_base::left);
cout << it->first << " - " << it->second << " " ;
if (++i >= 3 )
{
i = 0;
cout << endl;
}
}
cout << endl;
}
int get_words(map<string, int >& wordcounts)
{
string line;
string cleaned_word;
int count_new = 0;
cin >> line;
while (cin)
{
wordcounts[line]++;
cin >> line;
}
print_words(wordcounts, count_new);
return count_new;
}
int main()
{
mapType wordcounts;
get_words(wordcounts);
return 0;
}
Topic archived. No new replies allowed.