// G Hagopian

#include "..\std_lib_facilities-10.h"

using namespace std;

//------------------------------------------------------------------------------

struct Point
{
    int x;
    int y;

    friend istream& operator>>(istream& ist, Point& p)
    {
        char a,b,c;

        if ((ist >> a >> p.x >> b >> p.y >> c) && !(a == '(' && b == ',' && c == ')'))
            throw runtime_error("Invalid format");

        return ist;
    }

    friend ostream& operator<<(ostream& ost, const Point& p)
    {
        return ost << '(' << p.x << ',' << p.y << ')' << endl;
    }
    bool operator==(const Point& rhs) {
        return (x==rhs.x && y==rhs.y);
    }
    bool operator!=(const Point& rhs) {
        return !operator==(rhs);
    }
};

//------------------------------------------------------------------------------

int main()
try
{
    /*cout << "Please enter  file name: ";
    string name;
    cin >> name;
    ifstream ist(name.c_str());    // ist is an input stream for the file named name
    if (!ist) error("can't open input file ",name);*/
    int numPts{7};
    vector<Point> originalPoints;
    Point p;
    cout << "\nPlease enter " << numPts << " points: ";
    while ( originalPoints.size()<numPts && cin>>p) originalPoints.push_back(p);
    for(auto i: originalPoints) cout << i; //drill q3

    ///Drill 4
    string pFileName;
    cout << "\nEnter a name for the file: ";
    cin >> pFileName;
    ofstream ost(pFileName.c_str());
    if(!ost) error("can't open output file ", pFileName);

    for(auto i: originalPoints) ost << i;

    ///Drill 5
    vector<Point> processedPoints;
    ost.close();
    ifstream ist(pFileName.c_str());    // ist is an input stream for the file named name
    if (!ist) error("can't open input file ",pFileName);
    while(ist>>p) processedPoints.push_back(p);
    cout << "\nprocessedPoints contains\n";
    for(auto i: processedPoints) cout << i << endl;
    if(processedPoints.size()!=originalPoints.size()) cout << "\nSomething's wrong!";
    for(int i=0; i<originalPoints.size();++i) if(originalPoints[i]!=processedPoints[i])
        cout << "\nSomething's wrong!";

}
catch (exception& e) {
    cerr << "error: " << e.what() << '\n';
    return 1;
}
catch (...) {
    cerr << "Oops: unknown exception!\n";
    return 2;
}

//------------------------------------------------------------------------------

/*
Please enter 7 points: (1,1) (2,2) (3,3) (4,4) (5,5) (6,6) (7,7)
(1,1)
(2,2)
(3,3)
(4,4)
(5,5)
(6,6)
(7,7)
*/
