Ceasar Cipher

All I want to do at this point is output the letter used most. I can output a-z and how many thimes they are used in an encoded file, but not the single most used letter (which is q) Help please! Anyone!

#include <iostream>
#include <fstream>
#include <cctype>

using namespace std;



void initialize (int& lc, int list[], int maxIndex);
void copyText (ifstream& intext, ofstream& outtext, char& ch, int list[]);
void characterCount (char ch, int list []);
void writeTotal(ofstream& outtext, int lc, int list[], int maxIndex);
int indexLargestElement(const int list[], int listSize);


int main ()
{
// Declarations
int shift;
int lineCount;
int letterCount[26];
char ch;
int maxIndex = 0; // string inputFileName;///
ifstream inputFile;
ofstream outputFile;

// cout << "Input file name: ";
// getline(cin, inputFileName);

// Open the input file.
inputFile.open("C:\\Users\\bloo\\Desktop\\Erin\\codetext.txt"); // Need .c_str() to convert a C++ string to a C-style string
// Check the file opened successfully.
if (!inputFile)
{
cout << "Unable to open input file." << endl;
cout << "Press enter to continue...";
return 1;
}
outputFile.open("C:\\Users\\bloo\\Desktop\\Erin\\b.txt");
initialize(lineCount, letterCount, maxIndex);
inputFile.get(ch);


while (inputFile)
{
copyText(inputFile, outputFile, ch, letterCount);
lineCount++;
inputFile.get(ch);
}


writeTotal (outputFile, lineCount, letterCount, maxIndex);


inputFile.close();
outputFile.close();

return 0;
}

void initialize(int& lc, int list[], int maxIndex)
{
int j;
lc = 0;
maxIndex = 0;


for (j=0; j<26; j++)
list [j] = 0;
}

void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[])
{
while (ch != '\n')
{
outtext <<ch;
characterCount (ch, list);
intext.get(ch);
}
outtext << ch;
}

void characterCount (char ch, int list[])
{
int index;

ch = toupper (ch);

index = static_cast<int>(ch)-static_cast<int>('A');

if (0<= index && index <26)
list[index]++; //each letter count

}
/////shift void calcShift( int& shift, int list[]); should be here

int indexLargestElement(const int list[], int listSize, int maxIndex)
{
int index;


for (index = 1; index < listSize; index++)
if (list[maxIndex])
maxIndex = index;

return maxIndex;
}

void writeTotal(ofstream& outtext, int lc, int list[], int maxIndex{
int index;

outtext<< endl << endl;
outtext << "The number of lines = " << lc << endl;
outtext << "The most large number = " << maxIndex << endl;


for (index = 0; index <26; index++)
outtext << static_cast<char>(index + static_cast<int> ('A'))
<< "count = " << list[index] <<endl;
}
Last edited on
outtext << "The most large number = " << maxIndex << endl;
ugh...


Also to find the letter that is used the most just iterate over the letter counts and find the one that has the largest value. You can alternatively use std::max_element http://www.cplusplus.com/reference/algorithm/max_element/
Topic archived. No new replies allowed.