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...