Hi, im new to c++ and im trying to make a codebreaking program which works by reading a text file, working out which letter has the highest frequency and swapping it with e, etc.
Except i cant work out why it wont work lol, it runs untill the end of the codebreak function
Thanks for any help
Code:
#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[512])
//This creates a function called codebreak, which uses the input code[]
{
int x, lettotal, y, i, maxno, let[25];
//This creates all the integer variables used in my program, x, y and i are used for counting
char decry[25], broke[512];
//This creates the array used to hold the correct order of the alphabet
i = 0;
maxno = 0;
x = 0;
lettotal = 0;
y = 0;
//This sets all the integers used to 0
for (int x = 0; x <=25;){
let[x] = 0;
x = x + 1;
}
//This assigns 0 to every value in the array let[25]
for (int x = 0; x <= 512; x++){
if (code[x] = 'a'){
let[0] = let[0] + 1 ;
}
if (code[x] = 'b'){
let[1] = let[1] + 1 ;
}
if (code[x] = 'c'){
let[2] = let[2] + 1 ;
}
if (code[x] = 'd'){
let[3] = let[3] + 1 ;
}
if (code[x] = 'e'){
let[4] = let[4] + 1 ;
}
if (code[x] = 'f'){
let[5] = let[5] + 1 ;
}
if (code[x] = 'g'){
let[6] = let[6] + 1 ;
}
if (code[x] = 'h'){
let[7] = let[7] + 1 ;
}
if (code[x] = 'i'){
let[8] = let[8] + 1 ;
}
if (code[x] = 'j'){
let[9] = let[9] + 1 ;
}
if (code[x] = 'k'){
let[10] = let[10] + 1 ;
}
if (code[x] = 'l'){
let[11] = let[11] + 1 ;
}
if (code[x] = 'm'){
let[12] = let[12] + 1 ;
}
if (code[x] = 'n'){
let[13] = let[13] + 1 ;
}
if (code[x] = 'o'){
let[14] = let[14] + 1 ;
}
if (code[x] = 'p'){
let[15] = let[15] + 1 ;
}
if (code[x] = 'q'){
let[16] = let[16] + 1 ;
}
if (code[x] = 'r'){
let[17] = let[17] + 1 ;
}
if (code[x] = 's'){
let[18] = let[18] + 1 ;
}
if (code[x] = 't'){
let[19] = let[19] + 1 ;
}
if (code[x] = 'u'){
let[20] = let[20] + 1 ;
}
if (code[x] = 'v'){
let[21] = let[21] + 1 ;
}
if (code[x] = 'w'){
let[22] = let[22] + 1 ;
}
if (code[x] = 'x'){
let[23] = let[23] + 1 ;
}
if (code[x] = 'y'){
let[24] = let[24] + 1 ;
}
if (code[x] = 'z'){
let[25] = let[25] + 1 ;
}
//This whole loops is used to count the frequency of letters in the given text and store the
//values in the array called let[25]
}
while (y <= 25){
lettotal = lettotal + let[y];
y = y + 1;
}
//This works out the total numbers of letters used and stores it in the integer lettotal
y = 0;
while (y <= 25){
let[y] = let[y]/lettotal;
y = y + 1;
}
while (y <= 25){
while (i <= 25){
if (let[i] > maxno){
maxno = i;
}
//This code checks to see if the item in the array is currently larger than the maximum
//number(maxno)
i = i+1;
}
if (y = 0){
decry[maxno] = 'e';
}
if (y = 1){
decry[maxno] = 't';
}
if (y = 2){
decry[maxno] = 'a';
}
if (y = 3){
decry[maxno] = 'o';
}
if (y = 4){
decry[maxno] = 'i';
}
if (y = 5){
decry[maxno] = 'n';
}
if (y = 6){
decry[maxno] = 's';
}
if (y = 7){
decry[maxno] = 'h';
}
if (y = 8){
decry[maxno] = 'r';
}
if (y = 9){
decry[maxno] = 'd';
}
if (y = 10){
decry[maxno] = 'l';
}
if (y = 11){
decry[maxno] = 'c';
}
if (y = 12){
decry[maxno] = 'u';
}
if (y = 13){
decry[maxno] = 'm';
}
if (y = 14){
decry[maxno] = 'w';
}
if (y = 15){
decry[maxno] = 'f';
}
if (y = 16){
decry[maxno] = 'g';
}
if (y = 17){
decry[maxno] = 'y';
}
if (y = 18){
decry[maxno] = 'p';
}
if (y = 19){
decry[maxno] = 'b';
}
if (y = 20){
decry[maxno] = 'v';
}
if (y = 21){
decry[maxno] = 'k';
}
if (y = 22){
decry[maxno] = 'j';
}
if (y = 23){
decry[maxno] = 'x';
}
if (y = 24){
decry[maxno] = 'q';
}
if (y = 25){
decry[maxno] = 'z';
}
let[maxno] = 0;
i = 0;
y = y + 1;
}
//This group of if statements is used to find out the correct sequence in the alphabet, by replacing the
//letter with the highest frequency with e, etc.
x = 0;
for (int x = 0; x <= 512;){
if (code[x] != 'a'){
code[x] = decry[0];
}
if (code[x] != 'b'){
code[x] = decry[1];
}
if (code[x] != 'c'){
code[x] = decry[2];
}
if (code[x] != 'd'){
code[x] = decry[3];
}
if (code[x] != 'e'){
code[x] = decry[4];
}
if (code[x] != 'f'){
code[x] = decry[5];
}
if (code[x] != 'g'){
code[x] = decry[6];
}
if (code[x] != 'h'){
code[x] = decry[7];
}
if (code[x] != 'i'){
code[x] = decry[8];
}
if (code[x] != 'j'){
code[x] = decry[9];
}
if (code[x] != 'k'){
code[x] = decry[10];
}
if (code[x] != 'l'){
code[x] = decry[11];
}
if (code[x] != 'm'){
code[x] = decry[12];
}
if (code[x] != 'n'){
code[x] = decry[13];
}
if (code[x] != 'o'){
code[x] = decry[14];
}
if (code[x] != 'p'){
code[x] = decry[15];
}
if (code[x] != 'q'){
code[x] = decry[16];
}
if (code[x] != 'r'){
code[x] = decry[17];
}
if (code[x] != 's'){
code[x] = decry[18];
}
if (code[x] != 't'){
code[x] = decry[19];
}
if (code[x] != 'u'){
code[x] = decry[20];
}
if (code[x] != 'v'){
code[x] = decry[21];
}
if (code[x] != 'w'){
code[x] = decry[22];
}
if (code[x] != 'x'){
code[x] = decry[23];
}
if (code[x] != 'y'){
code[x] = decry[24];
}
if (code[x] != 'z'){
code[x] = decry[25];
}
x = x + 1;
}
//This loop is used to replace every letter in the array code[] with the correct replacement
//according to frequency
for (int x = 0; x <=512;){
broke[x] = code[x];
x = x + 1;
}
cout << "\nAfter code breaking:\n" << broke << endl;
}
int main()
//This defines the main() function
{
char file[256];
char code[512];
int i;
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);
inFile.close();
return 0;
}
for (int x = 0; x <= 512; x++){
if (code[x] = 'a'){
let[0] = let[0] + 1 ;
}
if (code[x] = 'b'){
....
This can be written as
Code:
for (int x = 0; x <= 512; x++){
if (code[x] >= 'a' && code[x] <= 'z') {
let[code[x]-'a']++;
}
}
Saves you MANY lines of code. Try to do similar things whereever else you have those constructs.
Code:
y = 0;
while (y <= 25){
let[y] = let[y]/lettotal;
y = y + 1;
}
This sets the whole let array back to zeros. Dividing an integer by a greater integer gives zero.
Last: Learn how to use your debugger and debug the program ;-)
Last edited by treuss; February 27th, 2008 at 09:29 AM.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
There are little people who properly use code tags in their first posting, very well!
There are some issues that needs some attention:
- does your alphabet contain only 25 letters? You´re using a 25-element array and then address the 26th element. In C++ arrays are indexed from 0 to N-1, so valid indexes for int code[512] are 0-511.
- you are using x as local variable and a loop variable, this may cause some confusion.
- name your variables properly. i,x,y and let are valid names, but no one will know what you are using them for.
- get rid of the huge switch-case statement. Just check if the current letter is >= 'a' and <= 'z'. If it is you can calculate the corresponding index by subtracting 'a' from the letter: iLetterIndex = code[x] - 'a'
- use uniform loops. There are 3 different notations which do all the same:
- for( int x = 0; x < y; x++ )
- for( int x = 0; x < y ) { x++ };
- int x = 0; while( x < y ) { x++ }
- you´re accumulating the total number of letters used and then divide the individual letter count by the sum. Because you´re using integers the result will always be 0.
- you´re not resetting y to 0 before entering a while() loop
Btw my let[25] array is 25 because an array starts from 0
You are mistaking.
Code:
char arr[2]; // Array of size two
arr[0] = 'a'; // first element
arr[1] = 'b'; // second element
arr[2] = 'c'; // illegal access !!
char let[25]; // array of size 25
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Yes you were right, the let is working but the decry array is not, i have attatched a screenshot of what happens when its run, the first group of 26 is the let array and the second is the decry array
- you´re not resetting y to 0 before entering a while() loop
Please try to work through what people already posted. Then post your new code with the new error.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Sorry, I had already made all the changes you said before, heres the new code
Code:
#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, lettotal, y, i, maxno, let[26];
//This creates all the integer variables used in my program, x, y and i are used for counting
char decry[26], broke[513];
//This creates the array used to hold the correct order of the alphabet
i = 0;
maxno = 0;
x = 0;
lettotal = 0;
y = 0;
//This sets all the integers used to 0
for (int x = 0; x <=25;){
let[x] = 0;
x = x + 1;
}
//This assigns 0 to every value in the array let[25]
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
y = 0;
i = 0;
while (y <= 25){
while (i <= 25){
if (let[i] > maxno){
maxno = i;
}
//This code checks to see if the item in the array is currently larger than the maximum
//number(maxno)
i++;
}
if (y = 0){
decry[maxno] = 'e';
}
if (y = 1){
decry[maxno] = 't';
}
if (y = 2){
decry[maxno] = 'a';
}
if (y = 3){
decry[maxno] = 'o';
}
if (y = 4){
decry[maxno] = 'i';
}
if (y = 5){
decry[maxno] = 'n';
}
if (y = 6){
decry[maxno] = 's';
}
if (y = 7){
decry[maxno] = 'h';
}
if (y = 8){
decry[maxno] = 'r';
}
if (y = 9){
decry[maxno] = 'd';
}
if (y = 10){
decry[maxno] = 'l';
}
if (y = 11){
decry[maxno] = 'c';
}
if (y = 12){
decry[maxno] = 'u';
}
if (y = 13){
decry[maxno] = 'm';
}
if (y = 14){
decry[maxno] = 'w';
}
if (y = 15){
decry[maxno] = 'f';
}
if (y = 16){
decry[maxno] = 'g';
}
if (y = 17){
decry[maxno] = 'y';
}
if (y = 18){
decry[maxno] = 'p';
}
if (y = 19){
decry[maxno] = 'b';
}
if (y = 20){
decry[maxno] = 'v';
}
if (y = 21){
decry[maxno] = 'k';
}
if (y = 22){
decry[maxno] = 'j';
}
if (y = 23){
decry[maxno] = 'x';
}
if (y = 24){
decry[maxno] = 'q';
}
if (y = 25){
decry[maxno] = 'z';
}
let[maxno] = 0;
i = 0;
y++;
maxno = 0;
}
//This group of if statements is used to find out the correct sequence in the alphabet, by replacing the
//letter with the highest frequency with e, etc.
x = 0;
for (int x = 0; x <= 512; x++){
if (code[x] != 'a'){
code[x] = decry[0];
}
if (code[x] != 'b'){
code[x] = decry[1];
}
if (code[x] != 'c'){
code[x] = decry[2];
}
if (code[x] != 'd'){
code[x] = decry[3];
}
if (code[x] != 'e'){
code[x] = decry[4];
}
if (code[x] != 'f'){
code[x] = decry[5];
}
if (code[x] != 'g'){
code[x] = decry[6];
}
if (code[x] != 'h'){
code[x] = decry[7];
}
if (code[x] != 'i'){
code[x] = decry[8];
}
if (code[x] != 'j'){
code[x] = decry[9];
}
if (code[x] != 'k'){
code[x] = decry[10];
}
if (code[x] != 'l'){
code[x] = decry[11];
}
if (code[x] != 'm'){
code[x] = decry[12];
}
if (code[x] != 'n'){
code[x] = decry[13];
}
if (code[x] != 'o'){
code[x] = decry[14];
}
if (code[x] != 'p'){
code[x] = decry[15];
}
if (code[x] != 'q'){
code[x] = decry[16];
}
if (code[x] != 'r'){
code[x] = decry[17];
}
if (code[x] != 's'){
code[x] = decry[18];
}
if (code[x] != 't'){
code[x] = decry[19];
}
if (code[x] != 'u'){
code[x] = decry[20];
}
if (code[x] != 'v'){
code[x] = decry[21];
}
if (code[x] != 'w'){
code[x] = decry[22];
}
if (code[x] != 'x'){
code[x] = decry[23];
}
if (code[x] != 'y'){
code[x] = decry[24];
}
if (code[x] != 'z'){
code[x] = decry[25];
}
}
//This loop is used to replace every letter in the array code[] with the correct replacement
//according to frequency
for (int x = 0; x <=512;){
broke[x] = code[x];
x++;
}
cout << "\nAfter code breaking:\n" << broke << endl;
for (int i=0; i<26; ++i){
cout << "" << i << ": " << let[i] << '\n';
}
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;
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);
inFile.close();
return 0;
}
Last edited by boburob; February 28th, 2008 at 06:59 AM.
After you filled the let array, your code stops to make sense. Please first describe for yourself what the decry array is supposed to contain, then write down a detailed step-by-step algorithm how you can create the array content.
And please make your code more readable by following the hints already posted:
Originally Posted by GNiewerth
- get rid of the huge switch-case statement. Just check if the current letter is >= 'a' and <= 'z'. If it is you can calculate the corresponding index by subtracting 'a' from the letter: iLetterIndex = code[x] - 'a'
- use uniform loops. There are 3 different notations which do all the same:
- for( int x = 0; x < y; x++ )
- for( int x = 0; x < y ) { x++ };
- int x = 0; while( x < y ) { x++ }
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Sorry im fairly new at c++ so alot of the stuff you said i didnt get:/
Code:
for (int x = 0; x <= 512; x++){
if (code[x] >= 'a' && code[x] <= 'z') {
let[code[x]-'a']++;
}
}
This works out a letter total from a text file
Code:
while (y <= 25){
while (i <= 25){
if (let[i] > maxno){
maxno = i;
}
//This code checks to see if the item in the array is currently larger than the maximum
//number(maxno)
i++;
}
if (y = 0){
decry[maxno] = 'e';
}
if (y = 1){
decry[maxno] = 't';
}
if (y = 2){
decry[maxno] = 'a';
}
if (y = 3){
decry[maxno] = 'o';
}
if (y = 4){
decry[maxno] = 'i';
}
if (y = 5){
decry[maxno] = 'n';
}
if (y = 6){
decry[maxno] = 's';
}
if (y = 7){
decry[maxno] = 'h';
}
if (y = 8){
decry[maxno] = 'r';
}
if (y = 9){
decry[maxno] = 'd';
}
if (y = 10){
decry[maxno] = 'l';
}
if (y = 11){
decry[maxno] = 'c';
}
if (y = 12){
decry[maxno] = 'u';
}
if (y = 13){
decry[maxno] = 'm';
}
if (y = 14){
decry[maxno] = 'w';
}
if (y = 15){
decry[maxno] = 'f';
}
if (y = 16){
decry[maxno] = 'g';
}
if (y = 17){
decry[maxno] = 'y';
}
if (y = 18){
decry[maxno] = 'p';
}
if (y = 19){
decry[maxno] = 'b';
}
if (y = 20){
decry[maxno] = 'v';
}
if (y = 21){
decry[maxno] = 'k';
}
if (y = 22){
decry[maxno] = 'j';
}
if (y = 23){
decry[maxno] = 'x';
}
if (y = 24){
decry[maxno] = 'q';
}
if (y = 25){
decry[maxno] = 'z';
}
let[maxno] = 0;
i = 0;
y++;
maxno = 0;
}
This code works out which letter in the text had the highest frequency, then it assigns e, the highest appearing letter in the alphabet, to the letters place in a new array decry, and continues doing this through this list
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.