Hi all,
For all those who are knowleadgeable enough I have a question for you. I am creating a program for my assignment in my Data Structures course and a piece of this assignment requires me to take in a file and analyze the existence and frequency of letters in my file. This is what I have so far:
Code:
map<char, int> m;
map<char, int>::iterator MI;
char ch;
char c;
// Open file
ifstream infile(argv[1], ios::in);
if (!infile) {
cerr << "File could not be opened" << endl;
exit(1);
}
// Populate map with letters and count the number of times it appears
while(infile.get(ch)){
c = ch;
int sum = 0;
// check if the buffer variable ch is a key in the map
for(MI = m.begin(); MI != m.end(); MI++)
{
if(MI->first == c){
// ch MUST be an existing key, simply increment
// corresponding counter value
sum = MI->second;
m[c] = sum++;
}
else {
// ch MUST be a new key and thus we need to
// allocate a new map for this key/value pair
m[c] = 1;
}
}
}
infile.close();
// output contents of map m
for (MI = m.begin(); MI != m.end(); MI++)
{
cout << "key: " << MI->first;
cout << "value: " << MI->second << endl;
}
My program compiles and I do not know if I am missing something logically but as far as I can tell, it is not even entering the nested for loop. Please help.