Another problem. I am creating this program that reads in a text file and counts the words and how many times it occurs in there using the map class. Well, I got it mostly written, and I decided to test to see if it reads in the file, well I ran into a snag. While debugging, the string functions (they are in bold), are not doing their job or I'm not using them right. Can you all be check to see where I'm going wrong?
The input file I use is the Declaration of Independence. I can put up the file itself if requested.
The input file I use is the Declaration of Independence. I can put up the file itself if requested.
Code:
#include <iostream>
#include <map>
#include <string>
#include <fstream>
#include <cctype>
#define MAX 64
using namespace std;
class WordCount {
private:
map<string, int> a_map;
string newWord, currentLine;
size_t nextWord;
char filename[MAX];
public:
WordCount(char filename[]) {
ifstream infile(filename);
while (!infile.eof()) {
getline(infile, currentLine);
size_t start;
size_t end = 0;
while(!currentLine.empty()) {
start = currentLine.find_first_not_of(" ");
end = currentLine.find_first_of(" ");
newWord = currentLine.substr(start, end);
for(int i = 0; newWord[i] != '\0'; i++) {
if(!isalpha(newWord[i]))
newWord[i] = '\0';
if(isupper(newWord[i]))
newWord[i] = tolower(newWord[i]);
}
wordCounter(newWord);
}
}
}
void wordCounter(string newWord) {
a_map.find(newWord);
if(a_map[newWord] == 0)
a_map[newWord] = 1;
else
a_map[newWord] += 1;
}
void sortList() {
}
void displayAll() {
for(map<string,int>::iterator itr = a_map.begin(); itr != a_map.end() ; ++itr){
cout << itr->first << " " << itr->second << endl;
}
}
};
int main () {
//char fileName[MAX];
//cout << "Enter document name: ";
//cin >> fileName;
WordCount count("declaration.txt");
count.displayAll();
return 0;
}