A Function that gives two vector and a file as output

Dear guys,
I have written the following function. In my mind, this function reads in a .txt file, then gives out another .txt file, and at the same time creates two vectors for the use by other parts of the main program, function as follow:

void read_in_two_arrays(vector<double>& time_series, vector<double>& value_series)
{
// Opening the input file with two arrays.
ifstream in_stream;
ofstream out_stream;
char in_file_name[16], out_file_name[16];
cout << "Enter input file name: ";
cin >> in_file_name;
cout << "Enter test output file name: ";
cin >> out_file_name;
in_stream.open(in_file_name);
out_stream.open(out_file_name);

formatting_output(out_stream, 2);

double time, value;
in_stream >> time >> value;
while (!in_stream.eof())
{
time_series.push_back(time);
value_series.push_back(value);
in_stream >> time >> value;
};

// Output the two arrays for cross-checking.
for (int j=0; j<time_series.size(); j++)
{
out_stream << time_series[j] << " " << value_series[j] << endl;
};

in_stream.close();
out_stream.close();
}

There is no problem with compilation. In my main program it is something like this:


int main()
{
vector<double> T, V, SMA;
read_in_two_arrays(T, V);
simple_moving_average(V, 20, SMA);
output_vector(T, V, SMA);
return 0;
}

In which output_vector is another function which again involves opening and writing out another .txt file.

Now, I am not sure what's wrong, but it seems that during run-time the program stuck at the read_in_two_arrays(T, V) stage saying that the output file test.txt of this read_in_two_arrays(T, V) function has stopped working.

Can anyone tell me if I have made any mistakes?

Many thanks.

Best regards,
Bosco
2/3 possible problems:

cin >> in_file_name;

cin >> out_file_name;
crash if the user enters a file name that exceeds 16

while (!in_stream.eof())
infinite loop if another error occurs (like file cannot be opened)
Hi Coder and all,
I have checked again, I don't have the three problems above for I am the user myself and I have carefully avoided all these problems.
In fact, I have just checked that even the output file which I named as "test2.txt" for the read_in_arrays function is fine and correct. But somehow, a window will pop up saying that my .exe has stopped functioning...
Anymore ideas? Thanks in advance.
Regards,
Bosco
Hello guys,
Maybe its more likely this segment of code is correct, however since coder777 mentioned that there could be something wrong with infinite loop... I believe that perhaps the bug actually comes from the simple_moving_average function... I am posting it here too...

There is no problem with compilation and I believe that the below code is the most likely to be giving me trouble as it involves two loops. Can anyone please have a look if there is anything wrong with my loop here?

Thanks.

void simple_moving_average(vector<double> series, int periods, vector<double>& SMA)
{
for (int i=0; i<periods; i++)
{
SMA[i]=0;//Not enough information for calculating SMA, arbitrarily set the values to zero.
};

double sum;
for (int j=periods; j<series.size(); j++)
{
sum=0;//initialization for the calculation of every new SMA.
for (int k=0; k<periods; k++)
{
sum=series[j-k]+sum;
};
SMA[j]=sum/periods;
};
}

Regards,
Bosco

Bosco
Last edited on
The simple_moving_average() is fine as long as periods is within the boundaries of SMA otherwise it would of course crash.

most likely is that you provide a (valid) file name. Nevertheless the file cannot be opened because the file isn't at that place the program assumes where it is.

And like already said: not open = crash

The problem is the relative path. Do you know which directory the program uses when it tries to open a file?
Topic archived. No new replies allowed.