Runtime Error in I/O Exercise

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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;
}   





Nobody has yet replied to my question. My question was solved on another forum. I needed to clear the cin function before entering the output name on line 30. And "cout" needed to be changed to "ost" on line 35.
Topic archived. No new replies allowed.