Letter Count.txt

I am trying to print and find the most and least common occurrences of the characters in the file (letter_count.txt). This is what i got so far if someone can help me out that would be great thanks!

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int idMax(int totalLetters[], int SIZE)
{
int idMax = 0;

for (int i = 1; i < 26; i++)
{
if (totalLetters[idMax] < totalLetters[i])
idMax = i + 1;

}
return idMax;

}

int idMin(int totalLetters[], int SIZE)
{
int idMin = 0;

for (int i = 0; i < 26; i++)
{
if (totalLetters[idMin] < totalLetters[i])
idMin = i - 1;

}
return idMin;

}

int main()
{

string letterCount = ("letter_count.txt");

ifstream inFile;
char letter;
int totalLetters[256] = { 0 };
int n = 0;
char maxCharacter = 26;

inFile.open("letter_count.txt");

if (inFile.fail())
{
cout << "Error: letter_count.txt could not be found" << endl;

system("pause");
}

while (inFile.get(letter))
{
if (isalpha(letter))
{
letter = toupper(letter);
int i = letter - 'A';
totalLetters[i]++;
}
}

for (int i = 0; i < 26; i++)

{
cout << " " << char(i + 'A') << " Appears " << totalLetters[i] << " times" << endl;

}

char idMax = (totalLetters, 26);
cout << " The letter that appears the most is: " << char(idMax + 'A') << " Appears " << totalLetters[idMax] << endl;

char idMin = (totalLetters, 0);
cout << " The letter that appears the least is: " << char(idMin + 'A') << " Appears " << totalLetters[idMin] << endl;

system("pause");
}


This is what it should display:
The most common letter is E with 147 occurances.
The least common letter is J with 0 occurances.
Last edited on
To get a better response in the forum:
(a) put your code in code tags; (you can edit it immediately, using the "Format" menu, if it doesn't come up at first posting;
(b) actually state what your problem is; did the code compile? did it give wrong answers? or what?


These lines make no sense at all, as your compiler will tell you.
1
2
3
4
5
char Most = (totalLetters, Most);
cout << " The letter that appears the most is: " << (totalLetters +'A') << " Appears " << totalLetters[Most] << endl;

char Least = (totalLetters, Least);
cout << " The letter that appears the least is: " << (totalLetters +'A') << " Appears " << totalLetters[Least] << endl;


Please go back and fix them.

totalLetters is an array. What are you trying to do with totalLetters +'A'?

Most is the name of a function, so don't make it the name of a variable and, given that function returns an int, don't assign it to a char.

= (totalLetters, Least);
This makes zero sense. You can find out how functions are called by reading the tutorial on this site: http://www.cplusplus.com/doc/tutorial/functions/


If you add some error-checking flags to your compile command (e.g. -Wall -pedantic -Wextra) you will get some hint at syntactically-possible, but almost-certainly-not-intended, lines of code.


Your method won't work if there is more than one letter with the maximum count, and/or more than one letter with the minimum count. Going by the tiles in "Scrabble", several letters could well have zero count.
Last edited on
I fixed it up a little bit i still keep getting incorrect output
Actually, you've ignored almost the entirety of my post, @Usm6rn.

PUT YOUR CODE IN CODE TAGS (and preferably, to allow the thread to make sense, post corrected code BELOW, not above, any critique, or the latter will not make sense).

What, exactly, are you trying to do here?
char idMax = (totalLetters, 26);
You have a function called idmax, so why are you trying to create another variable with this name?
You expect an int here, so why are you using a char?
Is this how you call a function? (The answer's "no".) Did you read the link I referred you to? (The answer is also "no".)
Last edited on
Topic archived. No new replies allowed.