Hi, I am designing a code breaker program that works out letter frequency in a text file and tries to codebreak using the frequency that letters appear in the english alphabet, however at the moment it is only outputting whatever is at the end of the decry array.
Thank you for your help
#include <iostream>
#include <fstream>
//This section of the code includes the libaries needed to run the program
using namespace std;
// Im using standard namespaces through out the code
void codebreak(char code[513])
//This creates a function called codebreak, which uses the input code[]
{
int x = 0, i = 0, maxno = 0, let[26], maxpos = 0;
//This creates all the integer variables used in my program, x and i are used for counting
char decry[26], broke[513];
//This creates the array used to hold the correct order of the alphabet
const char ciphertable[] = "etaoinshrdlcumwfgypbvkjxqz";
//This defines an array called ciphertable which holds the letters of the alphabet in the
//frequency they occur
const char normaltable[] = "abcdefghijklmnopqrstuvwxyz";
//This defines the array called normaltable which holds the letters of the alphabet in the
//correct order
for (int x = 0; x <=25; x++){
let[x] = 0;
}
//This assigns 0 to every value in the array let[26]
for (int x = 0; x <= 512; x++){
if (code[x] >= 'a' && code[x] <= 'z') {
let[code[x]-'a']++;
}
}
//This section checks to see what charator is held in each element of the array code so I can work out the total letal frequncies
for (int x = 0; x <= 25; x++){
for (int i = 0; i <= 25; i++){
if (let[i] > maxno){
maxno = let[i];
maxpos = i;
}
//This code checks to see if the item in the array is currently larger than the maximum
//number(maxno), if it is it replaces maxno, and the address is stored in maxpos
}
decry[maxpos] = ciphertable[x];
let[maxpos] = 0;
maxno = 0;
maxpos = 0;
}
//This is used to find out the correct sequence in the alphabet, by replacing the
//letter with the highest frequency with e, etc.
for (int x = 0; x <= 512; x++){
for (int i = 0; i <=25; i++){
if (code[x] = normaltable[i]){
broke[x] = decry[i];
}
}
}
//This loop is used to replace every letter in the array broke[] with the correct replacement
//according to frequency
cout << "\nAfter code breaking:\n" << broke << endl;
for (int i=0; i<26; ++i){
cout << "" << i << ": " << decry[i] << '\n';
}
}
int main()
//This defines the main() function
{
char file[256];
char code[513];
int i = 0;
//This defines the arrays and integers used in the main function
ifstream inFile;
//This open the stream inFile
system("cls");
//This clears the output screen
cout << " Code Breaker!" << endl;
cout << "Please enter the name and extension of the file you want to decode:" << endl;
cin.getline ( file, 512, '\n');
cout << "The name entered was : " << file << endl;
//This code gets the user to enter the name of the file that they wish to decode
inFile.open(file);
//This opens the text file specified by the user
if (!inFile) {
cerr << "Unable to open file";
exit(1);
}
//The code checks to see if the file opened correctly, if not, the program will end
while(inFile >> code[i] && i < 512){
i = i + 1;
}
//This puts the data that was in the users file, into the array code
cout << "This file contains the code : \n \n" << code << "\n" << endl;
cout << "Now performing code breaking..." << endl;
codebreak (code);
//This runs the function codebreak on the data held in the array code
inFile.close();
//This closes the stream
return 0;
}