I am doing an exercise from Stroustrup's book. It is the drill from chapter 10 on input and output streams. The program I wrote compiles but crashes when it reaches the prompt for the output file name. My IDE (VC++ Express) simply says there is an unhandled exception yielding a runtime_error at a memory location. Clearly there is a problem with opening the output file stream but I have no idea what it could be. Help is very much appreciated.
#include "../../std_lib_facilities.h"
struct Point {
int x;
int y;
Point(int x_axis, int y_axis) : x(x_axis), y(y_axis) {}
};
int main ()
{
vector<Point> original_points;
vector<Point> processed_points;
int x = 0;
int y = 0;
string oname = " ";
string iname = " ";
cout << "Please enter the coordinates for each point.\n";
while (cin >> x >> y) {
original_points.push_back(Point(x,y));
}
for (int i = 0; i < original_points.size(); i++) {
cout << "(" << original_points[i].x << ", "<< original_points[i].y << ")" << endl;
}
cout << "Please enter the output file name.\n";
cin >> oname;
ofstream ost(oname.c_str());
if (!ost) error("output file can't be opened");
for (int i = 0; i < original_points.size(); i++) {
cout << original_points[i].x <<" " << original_points[i].y << endl;
}
cout << "Please enter the input file name.\n";
cin >> iname;
ifstream ist(iname.c_str());
if (!ist) error("input file can't be opened");
while (ist >> x >> y) {
processed_points.push_back(Point(x,y));
}
for (int i = 0; i < processed_points.size(); i++) {
cout << "(" << processed_points[i].x << ", "<< processed_points[i].y << ")" << endl;
}
keep_window_open();
return 0;
}