Hey forum, I'm very new to C++, and I need some help. I'm currently having trouble trying to write a Caesar cipher in C++. So far, I'm able to get it to read characters from a text file, and let it count the # each letter appears in an array. What I'm trying to do exactly is, find the most frequent letter, and then use that to find the shift. But before that, i want to display which letter that it was that was most frequent, and the number of times it was in the text.

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

using namespace std;

//Globals
const int ARRAY_SIZE = 128;
const int E_LOC = 4;
int list[ARRAY_SIZE] = {0};
string reply;
ofstream outputFileName;
ifstream inputFile;
char ch;
	
//Prototypes
void characterCount(char ch, int list[]); 
void calcShift( int& shift, int list[]); 
void writeOutput(ifstream &in, ofstream &out, int shift);

int main() {
	int shift = 0;
    string inputFileName;
    char ch;
	 
	//Input File Name
    cout << "Input file name: ";
    getline(cin, inputFileName);

    // Open the input file.
    inputFile.open(inputFileName.c_str());
    
	// Check the file opened successfully.
	if ( ! inputFile.is_open()) {
        cout << "Unable to open input file." << endl;
        cout << "Press enter to continue...";
        getline(cin, reply);
        exit(1);
    }
	//Open the output file.
	cout << "Output file name: ";
	getline(cin, inputFileName);
	outputFileName.open(inputFileName.c_str());

    // This section reads and echo's the file one character (byte) at a time.
    while (inputFile.peek() != EOF) 
	{
        inputFile.get(ch);
		characterCount(ch, list);
    }

	calcShift(shift, list);

	inputFile.clear();
    inputFile.seekg(0);

	inputFile.close();
	inputFile.open(inputFileName.c_str());   

	cout << endl;
	system("PAUSE");
		return 0;
}

void characterCount(char ch, int list[])
{
		if (ch >= 'a' && ch <= 'z')
		{
			int asciiCode = 0;
 
			asciiCode = (static_cast<int>(ch)); 
			list[asciiCode]++; 
			cout << ch;
		}
}


void calcShift( int& shift, int list[])
{
	int mostUsedLetter = list[0];
	int mUsedIndex;

//Loop to search for greatest count and location (index) of greatest count.
for(int i = 0; i < ARRAY_SIZE; i++) 
	{
		if(list[i] > mostUsedLetter)
		{
			mostUsedLetter = list[i];
			mUsedIndex = i;
			cout << endl << "The letter: " << static_cast<char>(mUsedIndex) << endl << " was shown " << mostUsedLetter << " times." << endl;
			//shift = mUsedIndex - E_LOC;
		}
		
	}
}

I just threw up a bunch of default headers first, and worked from there. This code isn't complete yet, but I'm going step by step and trying to fix my mistakes before i continue. The current problem I'm having at the moment is that when i execute the code, it displays all the characters normally, but at the point where i prompt it to display the most frequent letter, it displays the message twice. One for a and l. Why is it doing that? Feel free to flame me with feedback, I really need it. Advice on waht to do next would be nice too.

Hey forum, I'm very new to C++, and I need some help. I'm currently having trouble trying to write a Caesar cipher in C++. So far, I'm able to get it to read characters from a text file, and let it count the # each letter appears in an array. What I'm trying to do exactly is, find the most frequent letter, and then use that to find the shift. But before that, i want to display which letter that it was that was most frequent, and the number of times it was in the text.

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

using namespace std;

//Globals
const int ARRAY_SIZE = 128;
const int E_LOC = 4;
int list[ARRAY_SIZE] = {0};
string reply;
ofstream outputFileName;
ifstream inputFile;
char ch;
	
//Prototypes
void characterCount(char ch, int list[]); 
void calcShift( int& shift, int list[]); 
void writeOutput(ifstream &in, ofstream &out, int shift);

int main() {
	int shift = 0;
    string inputFileName;
    char ch;
	 
	//Input File Name
    cout << "Input file name: ";
    getline(cin, inputFileName);

    // Open the input file.
    inputFile.open(inputFileName.c_str());
    
	// Check the file opened successfully.
	if ( ! inputFile.is_open()) {
        cout << "Unable to open input file." << endl;
        cout << "Press enter to continue...";
        getline(cin, reply);
        exit(1);
    }
	//Open the output file.
	cout << "Output file name: ";
	getline(cin, inputFileName);
	outputFileName.open(inputFileName.c_str());

    // This section reads and echo's the file one character (byte) at a time.
    while (inputFile.peek() != EOF) 
	{
        inputFile.get(ch);
		characterCount(ch, list);
    }

	calcShift(shift, list);

	inputFile.clear();
    inputFile.seekg(0);

	inputFile.close();
	inputFile.open(inputFileName.c_str());   

	cout << endl;
	system("PAUSE");
		return 0;
}

void characterCount(char ch, int list[])
{
		if (ch >= 'a' && ch <= 'z')
		{
			int asciiCode = 0;
 
			asciiCode = (static_cast<int>(ch)); 
			list[asciiCode]++; 
			cout << ch;
		}
}


void calcShift( int& shift, int list[])
{
	int mostUsedLetter = list[0];
	int mUsedIndex;

//Loop to search for greatest count and location (index) of greatest count.
for(int i = 0; i < ARRAY_SIZE; i++) 
	{
		if(list[i] > mostUsedLetter)
		{
			mostUsedLetter = list[i];
			mUsedIndex = i;
			cout << endl << "The letter: " << static_cast<char>(mUsedIndex) << endl << " was shown " << mostUsedLetter << " times." << endl;
			//shift = mUsedIndex - E_LOC;
		}
		
	}
}

I just threw up a bunch of default headers first, and worked from there. This code isn't complete yet, but I'm going step by step and trying to fix my mistakes before i continue. The current problem I'm having at the moment is that when i execute the code, it displays all the characters normally, but at the point where i prompt it to display the most frequent letter, it displays the message twice. One for a and l. Why is it doing that? Feel free to flame me with feedback, I really need it. Advice on waht to do next would be nice too.

The mostUsedLetter variable is initially set to ZERO. The list storage area for the letter 'a' will almost always have a count greater than ZERO. Thus, 'a' will be listed since it's count is greater than ZERO. But it doesn't necessarily mean that 'a' is the most used letter. It just means that the count of 'a' is greater than ZERO.