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') {
	//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
	//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
	//This opens the text file specified by the user
	if (!inFile) {
		cerr << "Unable to open file";
	//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
	//This closes the stream
	return 0;

Recommended Answers

All 4 Replies

Your comments are excessive. Don't comment things that are obvious. You could safely remove most of your comments. They make the code less readable. Also, when you do use comments, be concise; you don't need complete sentences.

When you post C++ code, use code=cpp instead of just code.

Sorry for the criticism without responding to your questions but I think sometimes people are told by teachers to comment their code and they think they have to write a novel.

I didn't look too closely at your logic but this is at least part of your problem:

if (code[x] = normaltable[i]){

Should be:

if (code[x] == normaltable[i]){

Thankyou for your help it is now working, also there are excessive comments becauses this is a university project lol

Thankyou for your help it is now working, also there are excessive comments becauses this is a university project lol

No problem.

Shouldn't matter what kind of project it is. If they are demanding this many comments, they are wrong. The purpose of comments is to make your code easier to understand. Excessive comments have the opposite effect. They should also be concise.

The only places you need comments in your code are a couple places to describe your logic in places that may not be immediately obvious and one to state what your function does.

Also, put comments before the code they explain.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.