'SEGMENTATION FAULT!' and arrays[char wtf]

well... WTF? I have looked over my code many-a-time and found absolutely no reason that it should overflow the arrays, insert an invalid character, or otherwise. I wouls like your opinion on the matter.

I'm about toi go to bed so i want to leave with some questions:

a) what are all the possible reasons this error could occur?

b) why the hell does the debug say "x = (some unrealistically large and unreal number)" when x has set parameters it can not go beyond?

c) Why does the Debug show all this random crap about my array when the items in the array should be shown?

I'm about to post my code, i want you to run it in debug for me and see what you think. Because i am self taught, i will very very much appreciate your help.

This was made in Code::Blocks:

main.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include "te_comf.h"
#include <string>
#include <windows.h>
#include <conio.h>
#include <fstream>
using namespace std;

int encode_text()
{
    cls();
    string f_name;
    cout<< "Name the save file:  ";
    getline(cin, f_name);
    while(f_name == "")
    {
        getline(cin, f_name);
    }
    f_name = (f_name + ".txt");
    ifstream icheck;
    icheck.open(f_name.c_str(), ios::in);
    if(icheck.is_open())
    {
        icheck.close();
        cls();
        cout<< "You already have a save with that name, sorry!."<< endl;
        waituser();
        cls();
        _cl();
        return 0;
    }
    ofstream f;
    f.open("prim.txt", ios::out);
    f<< "";
    f.close();
    system("prim.txt");
    cls();
    rename("prim.txt", f_name.c_str());
    _cl();
    encode(f_name);
    cls();
    ifstream fch;
    fch.open(f_name.c_str(), ios::in);
    while(!fch.eof())
    {
        getline(fch, f_name);
        cout<< f_name<< endl;
    }
    fch.close();
    waituser();
    cls();
    _cl();
    return 0;
}

int main()
{
    int x = 0;
    char ch;
    while(x == 0)
    {
        cls();
        cout<< "           ENC0D3R Main Menu"<< endl<< endl<< endl;
        cout<< " 1- Encode a text file"<< endl;
        cout<< " [E5CAPE]- 3XIT"<< endl;
        _cl();
        ch = _getch();
        if(ch == '1')
        {
            _cl();
            encode_text();
            _cl();
            continue;
        }
        if(ch == 0x1b)
        {
            _cl();
            return 0;
        }
    }
    return 0;
}


TE_commonfuncts.cpp :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <windows.h>
#include <fstream>
#include <conio.h>
#include <ctime>
using namespace std;

int cls()
{
    system("CLS");
    return 0;
}

int _cl()
{
    Sleep(100);
    while(_kbhit()) _getch();
    return 0;
}

int waituser()
{
    cout<< endl<< endl<< endl<< endl<< endl<< endl;
    cout<< "Press any button to continue..."<< endl;
    _cl();
    while(!_kbhit())
    {
        continue;
    }
    return 0;
}

int encode(string f_name)
{
    char fil[10000];
    char alphabet[2][26];
    char ch;
    int x = 0, alpha, shift, cap;
    ifstream fi;
    fi.open(f_name.c_str(), ios::in);
    while(!fi.eof())
    {
        x++;
        fi.get(ch);
        fil[x] = ch;
    }
    fi.close();
    x++;
    fil[x] = '\0';
    x--;
    for(char y = 'a'; y <= 'z'; y++)
    {
        alpha++;
        alphabet[1][alpha] = y;
    }
    alpha = 0;
    for(char y = 'A'; y <= 'Z'; y++)
    {
        alpha++;
        alphabet[2][alpha] = y;
    }
    cls();
    srand((unsigned)time(0));
    shift = ((rand()%26) + 1);
    for(int c = 1; c <= x; c++)
    {
        ch = fil[c];
        for(int C = 1; C <= 26; C++)
        {
            if(alphabet[1][C] == ch)
            {
                alpha = C;
                cap = 1;
                break;
            }
            if(alphabet[2][C] == ch)
            {
                alpha = C;
                cap = 2;
                break;
            }
            if(C == 26)
            {
                alpha = 0;
            }
        }
        if(alpha == 0)
        {
            continue;
        }
        alpha = (alpha + shift);
        while(alpha > 26)
        {
            alpha = (alpha - 26);
        }
        fil[c] = (alphabet[cap][alpha]);
    }
    ofstream coded;
    coded.open(f_name.c_str(), ios::out);
    coded<< fil;
    coded.close();
    return 0;
}


te_comf.h :

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef TE_COMF_H_INCLUDED
#define TE_COMF_H_INCLUDED
using namespace std;

int cls();

int _cl();

int waituser();

int encode(string f_name);

#endif // TE_COMF_H_INCLUDED 


I am attempting to create a shifted alphabet encoder. The encoding technique i am using uses a 2d array: [1][1-26] is lowercase, and [2][1-26] is uppercase. I use for statements to set these values, as you probably can see. When it gets the data from the text file, it is supposed to find the character value for each character, match it with the alphabetic array, and then "shift" it. It will produce a random shift and add the shift to the beginning of the file for decoding. But before I can do anything, i need the darn thing to work. I'm getting sleepy and probably sounding less intelligent as I go along so i'm going to leave this for you guys to figure out, lol. Thank you in advanced.

Also: computer specs: windows 7, Code::Blocks, i think thats it...

good god, good night...
for(int C = 1; C <= 26; C++) <----- should be C=0; C < 26; c++
{
if(alphabet[1][C] == ch)
{
cap = 1;
break;

this is one of your errors. the array is only defiend up to 26 six spaces so the last space is 25 and on the last iteration of the loop your trying to access spot 26. remember arrays start at zero and count to 1 less then the length you also have this problem in the first dimension of your array by using 1 & 2 instead of 0 & 1.

im hoping that fixes your error cause im far to tired to be much more help then that.

also
for(char y = 'a'; y <= 'z'; y++)
{
alpha++; <---- alpha is being used here
alphabet[1][alpha] = y; <-------and here but
}
alpha = 0; <------this is the first time alpha is delcared i think
Last edited on
TY. I still get segmentation fault though and i cant figure out why...

Revised:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <windows.h>
#include <fstream>
#include <conio.h>
#include <ctime>
using namespace std;

int cls()
{
    system("CLS");
    return 0;
}

int _cl()
{
    Sleep(100);
    while(_kbhit()) _getch();
    return 0;
}

int waituser()
{
    cout<< endl<< endl<< endl<< endl<< endl<< endl;
    cout<< "Press any button to continue..."<< endl;
    _cl();
    while(!_kbhit())
    {
        continue;
    }
    return 0;
}

int encode(string f_name)
{
    char fil[10000];
    char alphabet[1][26];
    char ch;
    int x = -1, alpha = -1, shift, cap;
    ifstream fi;
    fi.open(f_name.c_str(), ios::in);
    while(!fi.eof())
    {
        x++;
        fi.get(ch);
        fil[x] = ch;
    }
    fi.close();
    x++;
    fil[x] = '\0';
    x--;
    for(char y = 'a'; y <= 'z'; y++)
    {
        alpha++;
        alphabet[0][alpha] = y;
    }
    alpha = -1;
    for(char y = 'A'; y <= 'Z'; y++)
    {
        alpha++;
        alphabet[1][alpha] = y;
    }
    cls();
    srand((unsigned)time(0));
    shift = ((rand()%26) + 1);
    for(int c = 0; c < x; c++)
    {
        ch = fil[c];
        for(int C1 = 0; C1 <= 25; C1++)
        {
            if(alphabet[0][C1] == ch)
            {
                alpha = C1;
                cap = 0;
                break;
            }
            if(alphabet[1][C1] == ch)
            {
                alpha = C1;
                cap = 1;
                break;
            }
            if(C1 == 25)
            {
                alpha = -1;
            }
        }
        if(alpha == -1)
        {
            continue;
        }
        alpha = (alpha + shift);
        while(alpha > 26)
        {
            alpha = (alpha - 26);
        }
        fil[c] = alphabet[cap][alpha];      // Seg fault happens when we get here
    }
    ofstream coded;
    coded.open(f_name.c_str(), ios::out);
    coded<< fil;
    coded.close();
    return 0;
}


im short on time (gotta go to school) so ill make it short:

It showing seg fault but it looks like it is decoding it...
char alphabet[1][26]; I think you want the first size to be 2 and not 1.

yep Peter87 is right you need to declare it as char alphabet[2][26]
TY! It works! or... "Drkxu iye, sd gybuc!!!" lol
There is somthing wrong still. A is showing up as capital or some other symbol...

test 1
lower cAse A: 'A' cApitAl cAse A: '&"

I've performed a lot of other tests as well. I am going to post the code. I would like you to debug it and help me out because this is killing me here, lol. You should enter this after the random shift portion: shift = 26;. This will cause it not to encode it at all, but still run it through the program. The only problem is with the letter a. I don't know why.

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include "te_comf.h"
#include <string>
#include <windows.h>
#include <conio.h>
#include <fstream>
using namespace std;

int encode_text()
{
    cls();
    string f_name;
    cout<< "Name the save file:  ";
    getline(cin, f_name);
    while(f_name == "")
    {
        getline(cin, f_name);
    }
    f_name = (f_name + ".txt");
    ifstream icheck;
    icheck.open(f_name.c_str(), ios::in);
    if(icheck.is_open())
    {
        icheck.close();
        cls();
        cout<< "You already have a save with that name, sorry!."<< endl;
        waituser();
        cls();
        _cl();
        return 0;
    }
    ofstream f;
    f.open("prim.txt", ios::out);
    f<< "";
    f.close();
    system("prim.txt");
    cls();
    rename("prim.txt", f_name.c_str());
    _cl();
    encode(f_name);
    cls();
    ifstream fch;
    fch.open(f_name.c_str(), ios::in);
    while(!fch.eof())
    {
        getline(fch, f_name);
        cout<< f_name<< endl;
    }
    fch.close();
    ifstream fc;
    fc.open(f_name.c_str(), ios::in);
    fc.close();
    waituser();
    cls();
    _cl();
    return 0;
}

int decode_text()
{
    string f_name;
    cls();
    _cl();
    cout<< "File to decode:  ";
    getline(cin, f_name);
    while(f_name == "")
    {
        getline(cin, f_name);
    }
    f_name = (f_name + ".txt");
    ifstream ck;
    ck.open(f_name.c_str(), ios::in);
    if(!ck.is_open())
    {
        cls();
        cout<< "That file can not be found."<< endl;
        waituser();
        cls();
        return 0;
    }
    ck.close();
    decode(f_name);
    cls();
    _cl();
    return 0;
}

int main()
{
    int x = 0;
    char ch;
    while(x == 0)
    {
        cls();
        cout<< "           ENC0D3R Main Menu"<< endl<< endl<< endl;
        cout<< " 1- Encode a text file"<< endl;
        cout<< " 2- Decode a text file"<< endl;
        cout<< " [E5CAPE]- 3XIT"<< endl;
        _cl();
        ch = _getch();
        if(ch == '1')
        {
            _cl();
            encode_text();
            _cl();
            continue;
        }
        if(ch == '2')
        {
            _cl();
            cls();
            decode_text();
            _cl();
            cls();
            continue;
        }
        if(ch == 0x1b)
        {
            _cl();
            return 0;
        }
    }
    return 0;
}


TE_commonfuncts.cpp :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <iostream>
#include <windows.h>
#include <fstream>
#include <conio.h>
#include <ctime>
using namespace std;

int cls()
{
    system("CLS");
    return 0;
}

int _cl()
{
    Sleep(100);
    while(_kbhit()) _getch();
    return 0;
}

int waituser()
{
    cout<< endl<< endl<< endl<< endl<< endl<< endl;
    cout<< "Press any button to continue..."<< endl;
    _cl();
    while(!_kbhit())
    {
        continue;
    }
    return 0;
}

int decode(string f_name)
{
    char fil[10000];
    char array_mod[10000];
    char alphabet[2][26];
    char ch;
    int shift, index = -1, fil_max, cap, letter;
    ifstream fch;
    fch.open(f_name.c_str(), ios::in);
    while(fch.good())
    {
        index++;
        fch.get(ch);
        fil[index] = ch;
    }
    fil_max = index;
    for(int x = 0; x <= fil_max; x++)   //we take out the shift indication sequence
    {
        index = (x + 4);
        array_mod[x] = fil[index];
    }
    fil_max = (fil_max - 4);
    for(int x = 0; x <= fil_max; x++)
    {
        fil[x] = array_mod[x];
    }
    index = fil_max;
    fil[index] = '\0';
    fch.close();
    ifstream fint;
    fint.open(f_name.c_str(), ios::in);
    index = -1;
    while(index < 2)
    {
        index++;
        fint>> shift;
    }
    fint.close();
    cls();
    fil_max--;
    index = -1;
    for(char c = 'a'; c <= 'z'; c++)     //filling the lowercase portion of out array
    {
        index++;
        alphabet[0][index] = c;
    }
    index = -1;
    for(char c = 'A'; c <= 'Z'; c++)      //filling the uppercase portion of our array
    {
        index++;
        alphabet[1][index] = c;
    }
    for(int x = 0; x <= fil_max; x++)
    {
        ch = fil[x];
        for(int x1 = 0; x1 <= 25; x1++)
        {
            if(ch == alphabet[0][x1])
            {
                cap = 0;
                letter = x1;
                break;
            }
            else if(ch == alphabet[1][x1])
            {
                cap = 1;
                letter = x1;
                break;
            }
            else
            {
                cap = -1;
            }
        }
        if(cap == -1)
        {
            continue;
        }
        letter = (letter - shift);
        while(letter <= 0)
        {
            letter = (letter + 26);
        }
        ch = alphabet[cap][letter];
        fil[x] = ch;
    }
    ofstream ff;
    f_name = ("decoded_" + f_name);
    ff.open(f_name.c_str(), ios::out);
    ff<< fil;
    ff.close();
    system(f_name.c_str());
    cls();
    _cl();
    return 0;
}

int encode(string f_name)
{
    char fil[10000];
    char alphabet[2][26];
    char ch;
    int x = -1, alpha = -1, shift, cap;
    ifstream fi;
    fi.open(f_name.c_str(), ios::in);
    while(!fi.eof())
    {
        x++;
        fi.get(ch);
        fil[x] = ch;
    }
    fi.close();
    x++;
    fil[x] = '\0';
    x--;
    alpha = -1;
    for(char y = 'a'; y <= 'z'; y++)
    {
        alpha++;
        alphabet[0][alpha] = y;
    }
    alpha = -1;
    for(char y = 'A'; y <= 'Z'; y++)
    {
        alpha++;
        alphabet[1][alpha] = y;
    }
    cls();
    srand((unsigned)time(0));
    shift = ((rand()%26) + 1);
    while(shift == 26)
    {
        srand((unsigned)time(0));
        shift = ((rand()%26) + 1);
    }
    for(int c = 0; c < x; c++)
    {
        ch = fil[c];
        for(int C1 = 0; C1 <= 25; C1++)
        {
            if(alphabet[0][C1] == ch)
            {
                alpha = C1;
                cap = 0;
                break;
            }
            else if(alphabet[1][C1] == ch)
            {
                alpha = C1;
                cap = 1;
                break;
            }
            else
            {
                cap = -1;
            }
        }
        if(cap == -1)
        {
            continue;
        }
        alpha = (alpha + shift);
        while(alpha > 26)
        {
            alpha = (alpha - 26);
        }
        ch = alphabet[cap][alpha];
        fil[c] = ch;
    }
    fil[x] = '\0';
    ofstream coded;
    coded.open(f_name.c_str(), ios::out);
    if(shift > 9)
    {
        coded<< shift<< " |";
    }
    if(shift < 10)
    {
        coded<< "0"<< shift<< " |";
    }
    coded<< fil;
    coded.close();
    return 0;
}
and finally te_comf.h :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef TE_COMF_H_INCLUDED
#define TE_COMF_H_INCLUDED
using namespace std;

int cls();

int _cl();

int waituser();

int encode(string f_name);

int decode(string f_name);

#endif // TE_COMF_H_INCLUDED 


I don't know why it is doiung this, so I really appreciate your help!
NVM. I figured it out when i just started making inputs of the entire alphabet (abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ) instead of making actual strings. The problem was in the way the shift was made.
Topic archived. No new replies allowed.