I'm trying to run this program and it works, but I would like to clean it up and make it more acceptable. One of the problems I am having is that when the program runs when I put p1 to 100 to print the random numbers it will print them, but then the program crashes. When I put p1 = 1300, it runs because that is the number of actual integers in the file I'm reading from. Here is the source code.
Code:
class Data1{
protected:
int *p1;
int size1;
public:
Data1();
Data1(int);
~Data1();
int randomData();
int printData();
}; // Data1
Data1::Data1(){
p1 = new int[1300];
size1 = 1300;
}
Data1::Data1(int x1){
p1 = new int[x1];
size1 = x1;
}
Data1::~Data1(){
delete [] p1;
}
Data1::randomData(){
int i;
for(i=0; i<size1; i++){
*(p1+i) = random(899) + 100;
}
return 0;
}
Data1::printData(){
int i;
cout << "\n";
for(i=0; i<size1; i++){
cout << "\t" << *(p1+i);
}
return 0;
}
class Data2a: public Data1{
protected:
int forever;
int ct;
public:
Data2a();
Data2a(int);
~Data2a();
int fileInput1();
int fileOutput1();
}; // data2
Data2a::Data2a(){
forever = 1;
ct = 0;
}
Data2a::Data2a(int n1){
p1 = new int[n1];
size1 = n1;
}
Data2a::~Data2a(){
delete[]p1;
}
Data2a::fileInput1(){
ifstream in1("nums.dat");
while(forever){
in1 >> *(p1+ct);
if(*(p1+ct) == -1) break;
ct++;
}
in1.close();
cout << "\n\n\tFile size = " << ct;
return ct;
}
Data2a::fileOutput1(){
for(int i=0; i<ct; i++){
cout << " " << *(p1+i);
if((i%15==0)&&(i>0)) cout << "\n";
if((i%150==0)&&(i>0)){
cout << "\n\n\tEnter to continue...\n";
getch();
} // if
} // FOR LOOP
return 0;
}
int main(){
clrscr();
Data1 *d1;
Data2a *d2;
d1 = new Data1;
d2 = new Data2a;
d1->randomData();
d1->printData();
cout << "\n\n";
d2->fileInput1();
cout << "\n\n";
d2->fileOutput1();
delete d1;
delete d2;
cout << "\n\n\n\tComplete";
return 0;
} //MAIN
Comment