I am attempting to define a class named Frequency by using map container as a data structure. The class is intended to read characters from the keyboard and count the frequency of each character that has been input.
This shows the UML diagram for the class Frequency. The function printFreqTable should print the frequency of each input character as shown in the sample output:
Enter your string without spaces: textEPF
Char Frequency
E *
P *
F *
t **
e *
x *
However, my implementation does not produce any outputs, and exists with code -1073741819. I was also wondering if there is a more efficient method of implementation that uses a map container.
#include <iostream>
using namespace std;
#include <map>
#include <string>
#include <vector>
class Frequency {
public:
Frequency();
void insertStringtoMap();
void printFreqTable();
void readString();
private:
typedef map <char, string, less <char > > mid;
mid CMap;
string s;
};
Frequency::Frequency()
{
readString();
}
void Frequency::readString()
{
cout << "Enter your string without spaces: ";
getline(cin, s);
cout << endl;
}
void Frequency::insertStringtoMap()
{
int si = s.size();
char str[50];
strcpy_s(str, s.c_str());
string freq = 0;
for (char ch = 65; ch <= 122; ch++) {
int c = 0;
for (int i = 0; i < si; i++)
{
if (ch == str[i])
{
c++;
}
}
if (c > 0)
{
for (int star = 0; star < c; star++)
{
freq = freq + "*";
}
CMap.insert(mid::value_type(ch, freq));
}
}
}
void Frequency::printFreqTable()
{
cout << "Char\tFrequency";
for (auto iter = CMap.cbegin(); iter != CMap.end(); ++iter)
{
cout << iter->first << "\t"
<< iter->second << "\n";
}
}
int main()
{
Frequency f;
f.insertStringtoMap();
f.printFreqTable();
}