Hey again guys. I got another noob mistake filled program. I have a program that needs to read a file and out put the occurrence of each letter aswell as show the max and min letters and occurrences of said letters. I've got it to read the file and display the occurrence of each letter but am having trouble setting up the program to get the minimum and maximum letters and numbers. I have looked around for help but majority of the solutions are way more advanced than what is expected of me for my skill level aswell as a lot that are helpful seem to be in c-strings when I can only use the standard c++ string class. Please advise. Thank you in advance.
// Lab 2.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
char mostFrequent(string text);
int main()
{
string letterCount = ("letter_count.txt");
ifstream inFile;
char letter;
int totalLetters[26] = { 0 };
int n = 0;
char maxCharacter=0;
//opens document and reads
inFile.open("letter_count.txt");
//Error message if document not found
if (inFile.fail())
{
cout << "Error: letter_count.txt could not be found" << endl;
system("Pause");
}
//Converts all letters to upper case to simplify
while (inFile.get(letter))
{
if (isalpha(letter))
{
letter = toupper(letter);
int i = letter - 'A';
totalLetters[i]++;
}
}
//array for displaying letters and times of appearence
for (int i = 0; i < 26; i++)
{
cout << " " << char(i + 'A') << " Appears " << totalLetters[i] << " times" << endl;
}
//function to get max letter and occurence
char mostFrequent(string text);
{
int max = 0;
int count = 0;
char maxCharcter;
for (char q = 'a'; q <= 'z'; q++)
{
int count = 0;
for (int i = 0; i<strlen("letter_count.txt"); i++)
{
if (letterCount[i] == q)
count++;
}
if (count>max)
{
max = count;
maxCharcter = q;
}
}
cout << max << endl;
cout << maxCharcter << endl;
system("Pause");
}
return 0;
}
The first part of the code is OK. It gives an array of 26 ints that represents the number of occurrences of each letter. So there is no need to re-read the file to get the min and max; just use the array :
1 2 3 4 5
int idMax = 0;
for (int i = 0; i < 26; i++)
if (totalLetters[idMax] < totalLetters[i])
idMax = i;
cout << " " << char(idMax + 'A') << " Appears " << totalLetters[idMax];
Thank you so much it helps a lot. Now I got the data to display properly. The only thing is now I need to have atleast one function in this code so I'll probably use what you gave me as a function instead.
//call to function max
idMax = max(totalLetters, SIZE);
cout << " The letter that appears the most is: " << char(idMax + 'A') << " Appears " << totalLetters[idMax] << endl;
//call to function min
idMin = min(totalLetters, SIZE);
cout << " The letter that appears the least is: " << char(idMin + 'A') << " Appears " << totalLetters[idMin] << endl;
system("Pause");
return 0;
}
//function for max letter count
int max(int totalLetters[], int SIZE)
{
int idMax=0;
for (int i = 0; i < 26; i++)
{
if (totalLetters[idMax] < totalLetters[i])
idMax = i;
}
return idMax;
}
//function for min letter count
int min(int totalLetters[],int SIZE)
{
int idMin=0;
for (int i = 0; i < 26; i++)
{
if (totalLetters[idMin] > totalLetters[i])
idMin = i - 1;
}
return idMin;
}