So, I'm trying to create a Caesar Cipher using C++. It's supposed to be able to calculate the shift depending on what letter appears the most (which would mean that it's "e"). It's also supposed to have wrap around text, which I've tried to think of a way to have it done, but I just can't understand how to do it in the first place.
So I got a lot of the code written, and I managed to compile it. However, when I run it, it doesn't actually do anything. It just copies whatever was in the original file, and put it in the outfile.
Any help is much appreciated!
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
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;
string filename;
char ch;
ifstream infile;
ofstream outfile;
//Get input file
cout << "Input file name: ";
getline(cin, filename);
infile.open(filename.c_str());
if (!infile.is_open()) { //if it doesn't exist, exit
cout << "Unable to open file or it doesn't exist." << endl;
return 1;
}
//Get output file
cout << "Output file name: ";
getline(cin, filename);
//open the outfile, if it doesn't exist, the program will automatically create one.
outfile.open(filename.c_str());
int list[ARRAYSIZE] = {0}; //Setting everything to 0
while (infile.peek() != EOF) //Until the end of file is reached...
{
infile.get(ch);
characterCount(ch, list); //Go count the character
}
//Rewind it
infile.clear();
infile.seekg(0);
calcShift (shift, list); //Calculate the shift based on the most characters counted
writeOutput(infile, outfile, shift); //Decypher and write to the other document
return 0;
}
void characterCount(char ch, int list[])
{
if (ch >= 'A' && ch <= 'z') //If the character is in the alphabet...
{
int asciiCode = 0;
asciiCode = static_cast<int>(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
largest = 0;
for (int i = 1; i < ARRAYSIZE; i++)
{
if (list[maxIndex] < list[i])
maxIndex = i; //If this is true, change the largest index
}
largest = list[maxIndex]; //When the maxIndex is found, then that has the largest number.
if (largest >= 65 && largest <= 90) //Calculate shift with E (for upper-case letters)
shift = largest - 69;
if (largest >= 97 && largest <= 122) //For lower-case letters (e)
shift = largest - 101;
}
void writeOutput(ifstream &infile, ofstream &outfile, int shift)
{
char ch;
int asciiCode = 0;
while (infile.peek() != EOF) { //Until it is the end of the file...
infile.get(ch); //Get the next character
if (ch >= 'A' && ch <= 'z') //If the character is in the alphabet...
{
asciiCode = static_cast<int>(ch); //Change it to the ASCII number
asciiCode += shift; //Do the shift
ch = static_cast<char>(asciiCode); //Change it to the shifted letter
}
outfile << ch; //Print to the outfile
}
}