Attached is my code. I posted this twice on accident earlier, and I didnt attach the code which caused the users discomfort.

My problem is that i keep getting a segmentation fault and I can't understand why. The problem occurs during the decryption part of the program. My other question is how can i make my decryption part shorter and less "for" and "if/else if" statements.

Attachments
/*
//Name: ________
//Program: program18_26.cpp
//Purpose: to decrypt rot13 using frequency substitution
//Date: 
//Date Modified: everyday
*/

#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<string>
#include<fstream>
#include<cstring>
using namespace std;

int decrypt(int length, string word)
{
int freq[26];
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
int g = 0;
int h = 0;
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int m = 0;
int n = 0;
int o = 0;
int p = 0;
int q = 0;
int r = 0;
int s = 0;
int t = 0;
int u = 0;
int v = 0;
int w = 0;
int x = 0;
int y = 0;
int z = 0;
int place_a = 0;
int place_b = 0;
int place_c = 0;
int place_d = 0;
int place_e = 0;
int place_f = 0;
int place_g = 0;
int place_h = 0;
int place_i = 0;
int place_j = 0;
int place_k = 0;
int place_l = 0;
int place_m = 0;
int place_n = 0;
int place_o = 0;
int place_p = 0;
int place_q = 0;
int place_r = 0;
int place_s = 0;
int place_t = 0;
int place_u = 0;
int place_v = 0;
int place_w = 0;
int place_x = 0;
int place_y = 0;
int place_z = 0;
char commoneng[26] = {'e', 't', 'a', 'o', 'i', 'n', 's', 'r', 'h', 'l', 'd', 'c', 'u', 'm', 'f', 'p', 'g', 'w', 'y', 'b', 'v', 'k', 'x', 'j', 'q', 'z'};



   
    
    
        // gets the frequencies and fills each respective array with the place each letter is found
        for(int it = 0; it < length; it++)
        {
            if(word[it] == 'a')
            {
                a++;       
            }
            else if( word[it] == 'b')
            {
                b++;
            }
            else if( word[it] == 'c')
            {
                c++;
            }
            else if( word[it] == 'd')
            {
                d++;               
            }
            else if( word[it] == 'e')
            {
                e++;
            }
            else if( word[it] == 'f')
            {
                f++;
            }
            else if( word[it] == 'g')
            {
                g++;
            }
            else if( word[it] == 'h')
            {
                h++;
            }
            else if( word[it] == 'i')
            {
                i++;
            }
            else if( word[it] == 'j')
            {
                j++;
            }
            else if( word[it] == 'k')
            {
                k++;
            }
            else if( word[it] == 'l')
            {
                l++; 
            }
            else if( word[it] == 'm')
            {
                m++;
            }
            else if( word[it] == 'n')
            {
                n++;
            }
            else if( word[it] == 'o')
            {
                o++;
            }
            else if( word[it] == 'p')
            {
                p++;
            }
            else if( word[it] == 'q')
            {
                q++;
            }
            else if( word[it] == 'r')
            {
                r++;
            }
            else if( word[it] == 's')
            {
                s++;
            }
            else if( word[it] == 't')
            {
                t++;
            }
            else if( word[it] == 'u')
            {
                u++;
            }
            else if( word[it] == 'v')
            {
                v++;
            }
            else if( word[it] == 'w')
            {
                w++;
            }
            else if( word[it] == 'x')
            {
                x++;
            }
            else if( word[it] == 'y')
            {
                y++;
            }    
            else if( word[it] == 'z')
            {
                z++; 
            }
        }// end for
        
 //makes arrays for each letter with the restrictions of the frequencies of each letter       
int a_array[a];
int b_array[b];
int c_array[c];
int d_array[d];
int e_array[e];
int f_array[f];
int g_array[g];
int h_array[h];
int i_array[i];
int j_array[j];
int k_array[k];
int l_array[l];
int m_array[m];
int n_array[n];
int o_array[o];
int p_array[p];
int q_array[q];
int r_array[r];
int s_array[s];
int t_array[t];
int u_array[u];
int v_array[v];
int w_array[w];
int x_array[x];
int y_array[y];
int z_array[z];


        // gets the place of the letters in the input and puts them into an array
        for(int mm = 0; mm < length; mm++)
        {
        
            if(word[mm] == 'a')
            {
                if(place_a < length)
                {
                 a_array[place_a] = mm;
                 place_a++;
                }        
            }
            else if( word[mm] == 'b')
            {
                if(place_b < length)
                {
                 b_array[place_b] = mm;
                 place_b++;
                }
            }
            else if( word[mm] == 'c')
            {
                if(place_c < length)
                {
                 c_array[place_c] = mm;
                 place_c++;
                }
            }
            else if( word[mm] == 'd')
            {
                if(place_d < length)
                {
                 d_array[place_d] = mm;
                 place_d++;
                }                
            }
            else if( word[mm] == 'e')
            {
                if(place_e < length)
                {
                 e_array[place_e] = mm;
                 place_e++;
                } 
            }
            else if( word[mm] == 'f')
            {
                if(place_f < length)
                {
                 f_array[place_f] = mm;
                 place_f++;
                } 
            }
            else if( word[mm] == 'g')
            {
                if(place_g < length)
                {
                 g_array[place_g] = mm;
                 place_g++;
                } 
            }
            else if( word[mm] == 'h')
            {
                if(place_h < length)
                {
                 h_array[place_h] = mm;
                 place_h++;
                } 
            }
            else if( word[mm] == 'i')
            {
                if(place_i < length)
                {
                 i_array[place_i] = mm;
                 place_i++;
                } 
            }
            else if( word[mm] == 'j')
            {
                if(place_j < length)
                {
                 j_array[place_j] = mm;
                 place_j++;
                } 
            }
            else if( word[mm] == 'k')
            {
                if(place_k < length)
                {
                 k_array[place_k] = mm;
                 place_k++;
                } 
            }
            else if( word[mm] == 'l')
            {
                if(place_l < length)
                {
                 l_array[place_l] = mm;
                 place_l++;
                } 
            }
            else if( word[mm] == 'm')
            {
                if(place_m < length)
                {
                 m_array[place_m] = mm;
                 place_m++;
                } 
            }
            else if( word[mm] == 'n')
            {
                if(place_n < length)
                {
                 n_array[place_n] = mm;
                 place_n++;
                } 
            }
            else if( word[mm] == 'o')
            {
                if(place_o < length)
                {
                 o_array[place_p] = mm;
                 place_o++;
                } 
            }
            else if( word[mm] == 'p')
            {
                if(place_p < length)
                {
                 p_array[place_p] = mm;
                 place_p++;
                } 
            }
            else if( word[mm] == 'q')
            {
                if(place_q < length)
                {
                 q_array[place_q] = mm;
                 place_q++;
                } 
            }
            else if( word[mm] == 'r')
            {
                if(place_r < length)
                {
                 r_array[place_r] = mm;
                 place_r++;
                } 
            }
            else if( word[mm] == 's')
            {
                if(place_s < length)
                {
                 s_array[place_s] = mm;
                 place_s++;
                } 
            }
            else if( word[mm] == 't')
            {
                if(place_t < length)
                {
                 t_array[place_t] = mm;
                 place_t++;
                } 
            }
            else if( word[mm] == 'u')
            {
                if(place_u < length)
                {
                 u_array[place_u] = mm;
                 place_u++;
                } 
            }
            else if( word[mm] == 'v')
            {
                if(place_v < length)
                {
                 v_array[place_v] = mm;
                 place_v++;
                } 
            }
            else if( word[mm] == 'w')
            {
                if(place_w < length)
                {
                 w_array[place_w] = mm;
                 place_w++;
                } 
            }
            else if( word[mm] == 'x')
            {
                if(place_x < length)
                {
                 x_array[place_x] = mm;
                 place_x++;
                } 
            }
            else if( word[mm] == 'y')
            {
                if(place_y < length)
                {
                 y_array[place_y] = mm;
                 place_y++;
                } 
            }    
            else if( word[mm] == 'z')
            {
                if(place_z < length)
                {
                 z_array[place_z] = mm;
                 place_z++;
                } 
            }
        }// end for

Attached is my code. I posted this twice on accident earlier, and I didnt attach the code which caused the users discomfort.

My problem is that i keep getting a segmentation fault and I can't understand why. The problem occurs during the decryption part of the program. My other question is how can i make my decryption part shorter and less "for" and "if/else if" statements.

The code is way too long and can be shortened considerably by using well crafted arrays.

int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
int g = 0;
int h = 0;
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int m = 0;
int n = 0;
int o = 0;
int p = 0;
int q = 0;
int r = 0;
int s = 0;
int t = 0;
int u = 0;
int v = 0;
int w = 0;
int x = 0;
int y = 0;
int z = 0;
int place_a = 0;
int place_b = 0;
int place_c = 0;
int place_d = 0;
int place_e = 0;
int place_f = 0;
int place_g = 0;
int place_h = 0;
int place_i = 0;
int place_j = 0;
int place_k = 0;
int place_l = 0;
int place_m = 0;
int place_n = 0;
int place_o = 0;
int place_p = 0;
int place_q = 0;
int place_r = 0;
int place_s = 0;
int place_t = 0;
int place_u = 0;
int place_v = 0;
int place_w = 0;
int place_x = 0;
int place_y = 0;
int place_z = 0;

Variables like these can almost ALWAYS be made into arrays, like this:

int numOccurenceInWord[26];
int place[26];

for (int i = 0; i < 26; i++)
{
    numOccurenceInWord[i] = 0;
    place[i] = 0;
}

Here's your code:

// gets the frequencies and fills each respective array with the place each letter is found
        for(int it = 0; it < length; it++)
        {
            if(word[it] == 'a')
            {
                a++;       
            }
            else if( word[it] == 'b')
            {
                b++;
            }
            else if( word[it] == 'c')
            {
                c++;
            }
            else if( word[it] == 'd')
            {
                d++;               
            }
            else if( word[it] == 'e')
            {
                e++;
            }
            else if( word[it] == 'f')
            {
                f++;
            }
            else if( word[it] == 'g')
            {
                g++;
            }
            else if( word[it] == 'h')
            {
                h++;
            }
            else if( word[it] == 'i')
            {
                i++;
            }
            else if( word[it] == 'j')
            {
                j++;
            }
            else if( word[it] == 'k')
            {
                k++;
            }
            else if( word[it] == 'l')
            {
                l++; 
            }
            else if( word[it] == 'm')
            {
                m++;
            }
            else if( word[it] == 'n')
            {
                n++;
            }
            else if( word[it] == 'o')
            {
                o++;
            }
            else if( word[it] == 'p')
            {
                p++;
            }
            else if( word[it] == 'q')
            {
                q++;
            }
            else if( word[it] == 'r')
            {
                r++;
            }
            else if( word[it] == 's')
            {
                s++;
            }
            else if( word[it] == 't')
            {
                t++;
            }
            else if( word[it] == 'u')
            {
                u++;
            }
            else if( word[it] == 'v')
            {
                v++;
            }
            else if( word[it] == 'w')
            {
                w++;
            }
            else if( word[it] == 'x')
            {
                x++;
            }
            else if( word[it] == 'y')
            {
                y++;
            }    
            else if( word[it] == 'z')
            {
                z++; 
            }
        }// end for

Here's your code using my arrays:

for(int it = 0; it < length; it++)
        {
            char letter = word[it];
            int letterIndex = letter - 'a';  // convert 'a' to 0, 'b' to 1, etc.
            
            numOccurenceInWord[letterIndex]++;
         }

Much shorter, eh? This comes up time and again in your program. Anything with 26 if statements, one for each letter, can almost assuredly be turned into a single statement using an array index.

My advice is this. Rewrite the program using a 3 or 4 letter alphabet ('a' through 'c' or 'd') in any way you feel comfortable (use if's, else if's, whatever), so it works. Then look at your code and see where you can change the if-statements into index arrays. But reducing the number of letters will cut the lines of code WAY down and make things much faster and easier to work with. Then when you've changed it to get rid of all of the unnecessary if statements, make it 26 letters again.

Comments
Hehe, your code looks a lot better :P
Sounds like a plan to me

Much shorter, eh? This comes up time and again in your program. Anything with 26 if statements, one for each letter, can almost assuredly be turned into a single statement using an array index.

That sounds perfect. so does the rest of your improvements. VernonDozier = GOD. haha. Could you show me how to do this single array you mention in the quote above?

That sounds perfect. so does the rest of your improvements. VernonDozier = GOD. haha. Could you show me how to do this single array you mention in the quote above?

I gave an example in my last post. It's a similar idea everywhere else. Glancing at your code, it looks like there may be some logic problems and possibly else if statements that should be if statements, but I'm not sure. The code is too long to get a real sense of the program flow. I would again reduce your code from a 26 letter alphabet to a 3 or 4 letter alphabet. This will drastically reduce the number of lines of code, which is what you want. Then when the program is a manageable size, change to arrays. It'll be much easier to change to arrays when it's smaller, and easier to spot any problems that CAN'T be changed (you may need other redesigns). But it's simply too big to work with now until you reduce the size. As an added bonus, once you reduce the size of the program, you can post it instead of attach it, so more people will look at it.

This article has been dead for over six months. Start a new discussion instead.