You asked about
specifically, so I'll stick to that. I haven't looked at the rest of your code in any detail, because something caught my eye straight away.
, you write
cout << "Error in opening file." << endl;
If everything has gone well to this point, this will always print the error message and exit! The reason is that you've conflated two things:
checks for an error, and so does
means "If there isn't
an error, then...