Wiem, wiem, problem był już w innym wątku, ale jego temat nie odpowiada temu zagadnieniu. jakby co, tamten temat można już usunąć ;)
Otóż mój kod kompiluje się na dev-c++, ale na ms visualu już nie. Musi mieć dynamicznie tworzone tablice. Czy ktoś mógłby mi pomóc, i pokazać gdzie i jakie poprawki wprowadzić? Podobno to kwestia tylko ok. trzech linijek, w miejscach deklaracji tablic, ja niestety mimo wielu prób nie dałem rady tego sam zrobić.
Wierzę, że i tym razem ktoś pomoże :)
Miejsca, gdzie wymagane sa poprawy zaznaczone przypiami.
Aha, potrzebny będzie do tego chyba jeszcze destruktor, nie?
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
class CData
{
private:
char *Name1;
char *Name2;
fstream File1;
fstream File2;
int x;
int y;
public:
int InFile(char *InName);
int OutFile(char *OutName);
void Encryption();
void Decryption();
void Dim(int& a, int& b);
void Close();
CData(void ){
Name1 = new char[128];
Name2 = new char[128];
}
~CData(){
delete[] Name1;
delete[] Name2;
}
};
int CData::InFile(char *InName){
strcpy(Name1, InName);
File1.open(Name1,ios::in);
if(File1.is_open()==true){
cout << endl << "Source accessed successfully!" << endl;
}
if(!File1){
cout << endl << "Unable to access source file" << Name1 << endl;
return 1;
}
return 0;
}
int CData::OutFile(char *OutName){
strcpy(Name2, OutName);
File2.open(Name2,ios::out);
if(File2.is_open()==true){
cout << endl << "Target accessed successfully!" << endl;
}
else if(!File2){
cout << endl << "Unable to access target file" << Name2 << endl;
return 1;
}
return 0;
}
void CData::Dim(int& a, int& b){
x = a;
y = b;
}
void CData::Encryption(){
char tab[x][y]; // Deklaracja tablicy, do poprawy
int i,j;
for(i=0; i<x; i++){
for(j=0; j<y; j++){
tab[i][j]='\0';
}
}
i=j=0;
char zmienna;
zmienna = File1.get();
while (File1.good()){
i=j=0;
while((i<x) && (File1.good())){
tab[i][j]=zmienna;
zmienna = File1.get();
if(i%2==0) j++;
if(j==y){j--; i++;}
if(i%2==1) j--;
if(j==(-1)){j++; i++;}
}
i=j=0;
while (j<y){
while(i<x){
File2.put (tab[i][j]);
i++;
}
i=0;
j++;
}
for(i=0; i<x; i++){
for(j=0; j<y; j++){
tab[i][j]='\0';
}
}
}
}
void CData::Decryption(){
char tab[x][y]; // Deklaracja tablicy, do poprawy
int i,j;
for(i=0; i<x; i++){
for(j=0; j<y; j++){
tab[i][j]='\0';
}
}
char zmienna;
zmienna = File1.get();
while(File1.good()){
i=j=0;
while (j<y){
while ((i<x) && (File1.good())){
tab[i][j]=zmienna;
zmienna = File1.get();
i++;
}
i=0;
j++;
}
i=j=0;
while(i<x){
File2.put (tab[i][j]);
if(i%2==0) j++;
if(j==y){j--; i++;}
if(i%2==1) j--;
if(j==(-1)){j++; i++;}
}
for(i=0; i<x; i++){
for(j=0; j<y; j++){
tab[i][j]='\0';
}
}
}
}
void CData::Close(){
File1.close();
File2.close();
}
int main(){
CData Object;
char FileName1[128];
char FileName2[128];
int c;
int d;
int choise;
cout << "Enter source file name:"<<endl;
cin >> FileName1;
Object.InFile(FileName1);
cout << "Enter target file name:"<<endl;
cin >> FileName2;
Object.OutFile(FileName2);
cout <<"Enter X dimension:"<<endl;
cin >> c;
cout <<"Enter Y dimension:"<<endl;
cin >> d;
Object.Dim(c, d);
cout << "1. Encryption \n 2. Decryption"<<endl;
cin >> choise;
cout <<endl;
switch (choise){
case 1:
Object.Encryption();
break;
case 2:
Object.Decryption();
break;
default:
return 0;
break;
}
Object.Close();
system("PAUSE");
return 0;
}