goodluckmich
MIS
I wrote a C++ program in Dev-C++. I kept having the problem of access violation (segmentation fault) when I was running the program. The following was a segment at which the problem probably occurred.
int main(){
ofstream fout("output.txt");
for(int j=0;j<20;j++)
{
cout<<"SEED:"<<j+1<<endl;
srand(j+1);
testRun(fout);
}
fout.close();
system("pause");
return 0;
}
void testRun(ofstream& fout){
for (int s = 0; s < 1000; s++){
double parameter = l.getParameters(s);
float c=1;
float e=0.2;
int d=20;
if (fout.is_open())
{
fout << "step: \t" << s << "\n";
}
else
{
cout << "error!";
}
}
}
It seems the problem occurred at fout line, but I didn't get why. I tried to blocked the line "fout << "step: \t" << s << "\n";" and the program worked. However I really need to output the results to the file. Could anyone give me some suggestions? Thanks a lot!
int main(){
ofstream fout("output.txt");
for(int j=0;j<20;j++)
{
cout<<"SEED:"<<j+1<<endl;
srand(j+1);
testRun(fout);
}
fout.close();
system("pause");
return 0;
}
void testRun(ofstream& fout){
for (int s = 0; s < 1000; s++){
double parameter = l.getParameters(s);
float c=1;
float e=0.2;
int d=20;
if (fout.is_open())
{
fout << "step: \t" << s << "\n";
}
else
{
cout << "error!";
}
}
}
It seems the problem occurred at fout line, but I didn't get why. I tried to blocked the line "fout << "step: \t" << s << "\n";" and the program worked. However I really need to output the results to the file. Could anyone give me some suggestions? Thanks a lot!