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;
}

What lines does it crash in?
What is the output before it crashes?
What does the file look like?

@firstPerson,

1) The program crashes just after line 29. So say line 30.

2) The last output to the screen is the prompt to enter the name of the output file. That is line 29. Then a MS alter window pops up with the message of an unhandled exception. Even though the cursor still blinks in the IDE screen and the window is active, it does not accept any input from my keyboard.

3) The data to be written to the output file is a number of coordinates. I have been using 3 points, so the file would have 3 pairs of integers representing (x,y) coordinates, one pair per line. N.B. The output file doesn't exist because I was not able to open it.

>> The last output to the screen is the prompt to enter the name of the output file.

Once you've gotten out of the initial while() loop, I believe you use Ctrl+Z to stop entering coordinates, cin has eof bit set, which you must clear() before trying to get any other input (otherwise cin remains non-functional).

>> Then a MS alter window pops up with the message of an unhandled exception.

The exception gets thrown by the error() function because oname remains what it is initialized to (" "), look into std_lib_facilities.h for details. You might wrap the code in a try/catch block, i.e.

int main () try
{

  < all your code here >

}
catch(std::exception & e)
{
  // What does it say?
  cout << e.what() << endl;
}

The problem is with cin. This statement :

while (cin >> x >> y){...}

exits only when cin fails. And when cin fails, you cannot use it until you cleared its flags. Thats why cin >>oname fails.

To solve your problem, do this :

while (cin >> x >> y){
 //...
}
cin.clear(); //clear the flags
while(cin.get() != '\n'); //discard all the bad characters

for(...){...}
cin >> oname
//...and so on with the rest of your code

@mit,

Yes. I did use Ctrl+Z to exit the

while (cin >> x >> y);

loop. If I had used an exit condition like

if (x == -999 && y == -999) break;

would I still have had the problem with

cin >> oname

?


@first,

I will add the

cin.clear()

and

cin.get()

lines and get back to you this evening. I copied the example from my textbook and it did not mention

cin.clear()

or discuss the need to empty or reset the cin function before using it again.

Stroustrup does that a lot. He uses examples and has exercises that require knowledge not yet discussed. This business of finding landmines by walking on them is a frustrating way to learn.

BTW Will this also apply to the file input section,

cin >> iname

?

>>BTW Will this also apply to the file input section

No, once you clear the flags in cin, you do not need to clear it again, unless it fails again.

OK! I added the lines

cin.clear();
while (cin.get() != '\n');

and the program ran correctly. But only after correcting another error on line 35: "cout" should have been "ost".

Thank you very much for your help in this exercise. I am onto the next one!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.