It is not working but this is what I have. If this what you were referring too. I am taking your advice, or at least attempting to. I am going to try the map<string,int> myMap approach after I get this way to work.
Code:
#include "WordCount.h"
int main(void){
WordCount count;
std::string names[] = {"Drew","Mike","Alex","Drew","Mike"};
for(auto name: names){
count.checkWords(name);
}
count.checkWords(names[0]);
return 0;
}
Code:
#pragma once
#include <iostream>
#include <map>
class WordCount
{
private:
struct Word{
int count;
int wordOccurrance(){
return count++;
}
};
std::map<std::string, Word> words;
public:
WordCount(void);
~WordCount(void);
void checkWords(std::string s);
void printFrequency(std::string s);
};
Code:
#include "WordCount.h"
#include <unordered_map>
#include <string>
WordCount::WordCount(void)
{
}
WordCount::~WordCount(void)
{
}
void WordCount::checkWords(std::string s){
if(words.count(s)>0){
words.at(s).wordOccurrance;
}
else{
words.emplace (s,new Word);
}
}
void WordCount::printFrequency(std::string s){
if(words.at(s).wordOccurrance > 0){
int value = words.at(s).wordOccurrance;
std::cout<<"Word: "<<s<<" occurred "<<value<<" times.";
}else{
std::cout<<"The word does not exist.\n";
}
}