Caesar Cipher Using Voids

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. SO far my program opens the input file and decrypts the message in the output file which works fine. I am finding the shift value. What is not working is finding the most frequent letter.
Thank You in advance.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
 
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstring>

using namespace std;
const int ARRAYSIZE = 128;

void characterCount(char ch, int list[]);
void calcShift( int& shift, int list[]);
void writeOutput(ifstream &in, ofstream &out, int shift);

int main()
{
    int asciiCode = 0,
        shift = 0;
	char ch;
    string inputFileName;
    ifstream inputFile;
    ofstream outputFile;
	string reply;
    //Open input file
    cout << "Input file name: ";
    getline(cin, inputFileName);
    inputFile.open(inputFileName.c_str());
        if (!inputFile.is_open()) { 
            cout << "Unable to open file or it doesn't exist." << endl;
            return 1;
        }
    //output file
    cout << "Output file name: ";
    getline(cin, inputFileName);
    outputFile.open(inputFileName.c_str());
    int list[ARRAYSIZE] = {0}; 
        while (inputFile.peek() != EOF) 
        {
            inputFile.get(ch);
            characterCount(ch, list); 
        }
    inputFile.clear();
    inputFile.seekg(0);
    calcShift (shift, list); //Calculate the shift based on the characters counted
    writeOutput(inputFile, outputFile, shift); //Decypher and write to the other document
	inputFile.close();
	outputFile.close();
	cout << "Press enter to continue...";
	getline(cin, reply);
    return 0;	
}
void characterCount(char ch, int list[])
{
        if (ch >= 'A' && ch <= 'z') //If the character is in the alphabet...
        {
            int asciiCode = 0;
            asciiCode = (ch); //Change it to the ASCII number
            list[asciiCode]++; //And note it on the array
        }
}
void calcShift( int& shift, int list[])
{
    int maxIndex = 0; //Asuming that list[0] is the largest
	int mostFreqLetter;
    for (int i = 1; i < ARRAYSIZE; i++)
    {
        if (list[maxIndex] < list[i])
                maxIndex = i; //If this is true, change the largest index
    }
    if (maxIndex >= 'A' && maxIndex <= 'Z') //Calculate shift for for upper-case letters
        shift = 'E' - maxIndex;
    if (maxIndex >= 'a' && maxIndex <= 'z') //For lower-case letters 
        shift = 'e' - maxIndex;

	mostFreqLetter = 'e' - shift;
	
	cout << "Most frequent letter is " << shift << endl;
	cout << "Most is " << mostFreqLetter << endl;
}
void writeOutput(ifstream &inputFile, ofstream &outputFile, int shift)
{
    char ch;
    while (inputFile.peek() != EOF) { //Until it is the end of the file...
        inputFile.get(ch); //Get the next character
            if (ch >= 'A' && ch <= 'Z') //If the character is in the alphabet...
            {
                ch = 'A' + (((ch - 'A') + shift + 26) % 26);
            }
            if (ch >= 'a' && ch <= 'z') //If the character is in the alphabet...
            {
                ch = 'a' + (((ch - 'a') + shift + 26) % 26);
            }
        outputFile << ch; //Print to the outfile
    }  	
}

Last edited on
What you display as "Most frequent letter" is in fact your shift.
Topic archived. No new replies allowed.