I am importing object files and making tables from the for a project due tommorow night. The function works fine when I use it the first time i.e. input pro1.txt, but when I call the function again and input pro2.txt as the file name my program crashes. All of the text files are in the same folder as the code. Any help to where I screwed up would be greatly appreciated! My code is:
void readFile(Object pObject[], int &p){
double distance[100];
double height[100];
string name[7];
ifstream file;
string s;
int t=0;
cout << "Please enter the file name: ";
cin >> s;
cout<<endl;
file.clear();
file.open(s.c_str());
if (file.fail()){
cout << "ERROR: file does not exist or corrupted file!" <<endl;
}
else if (!file.fail()){
while(!file.eof()){
file >> name[p] >>distance[p] >>height[p];
if(!file.eof()){
t++;
p++;
}
}
file.close();
cout << " Object" << endl;
cout << "Distance " << "Height" << endl<<endl;
for (int q=0; q<p;q++) cout << setw(8) <<name[q] << setw(8) <<distance[q] <<setw(11)<< height[q] <<endl;
cout << endl;
cout << t <<" objects read"<<endl;
cout << p << " objects in database"<<endl<<endl;
}
}