In: Computer Science
b, prompt the user to input several (x,y) pairs. As the data is entered, store it in a vector of Points called orginal_points. Input is terminated with the EOF character (differs by OS type). To be clear, I am asking you to define an input stream operator to read the point format: (x,y). As an example, the input of (5,6) is the correct input format for a point. While the I/O format includes the ‘(‘, ‘,’, and ‘)’ characters, the internal storage contains only the x and y values. Please Note -- I am expecting a robust, user-friendly way to ensure Points are entered correctly and in a valid format. A simple termination of the program is not acceptable here -- If I’ve entered a number of valid points, don’t discard them -- recover from the error so that input can continue (if possible) and if input cannot continue, ensure that the program proceeds to save the points I have entered. Also important -- this input stream operator needs to work with standard input (cin) and formatted (text) file input streams.
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const Point& p2) { return this->x == p2.x and this->y == p2.y; } bool operator!=(const Point& p2) { return this->x != p2.x or this->y != p2.y; } friend ostream &operator<<( ostream &out, const Point &P ) { out << "(" << P.x << ", " << P.y << ")"; return out; } friend istream &operator>>( istream &in, Point &P ) { char d1, d2, d3; // input format: (1, 2) in >> d1 >> P.x >> d2 >> P.y >> d3; return in; } }; int main() { vector<Point> original_points; cout << "Enter points :\n"; Point P; while( cin >> P ) { original_points.push_back(P); } ofstream out("mydata.txt"); cout << "You entered the points:\n"; for(Point p: original_points) { cout << p << '\n'; out << p << '\n'; } out.close(); //pause cin.get(); char ch; cout << "Press enter to continue: "; getchar(); ifstream in("mydata.txt"); vector<Point> processed_points; while( in >> P ) { processed_points.push_back(P); } int n = original_points.size(); for(int i=0; i<n; i++) { if(original_points[i] == processed_points[i]) { cout << "Points at index " << i << " are same\n" << original_points[i] << " " << processed_points[i] << '\n'; } if(original_points[i] != processed_points[i]) { cout << "Points at index " << i << " are not same\n" << original_points[i] << " " << processed_points[i] << '\n'; } } } depend on this code write error handling code
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct Point{
int x, y;
bool operator==(const Point& p2) {
return this->x == p2.x and this->y == p2.y;
}
bool operator!=(const Point& p2) {
return this->x != p2.x or this->y != p2.y;
}
friend ostream &operator<<( ostream &out, const Point
&P ) {
out << "(" << P.x << ", " << P.y <<
")";
return out;
}
friend istream &operator>>( istream &in, Point
&P ) {
char d1, d2, d3;
// input format: (1, 2)
in >> d1 >> P.x >> d2 >> P.y >>
d3;
return in;
}
};
int main()
{
vector<Point> original_points;
cout << "Enter points :\n";
Point P;
// loop continues until any error occurs
while( cin >> P )
{
// if any input error occurs, exit the loop keeping the valid
points in vector
if(cin.fail())
{
cin.clear(); // back in 'normal' operation mode
cin.ignore(100,'\n'); // and remove the bad input
break;
}
original_points.push_back(P);
}
ofstream out("mydata.txt");
cout << "You entered the points:\n";
for(Point p: original_points)
{
cout << p << '\n';
out << p << '\n';
}
out.close();
//pause
//cin.get();
char ch;
cout << "Press enter to continue: ";
cin>>ch;
ifstream in("mydata.txt");
vector<Point> processed_points;
// loop to read the data from input file
while( in >> P )
{
// if input file read error occurs, exit the loop
if(in.fail())
{
in.clear(); // back in 'normal' operation mode
in.ignore(100,'\n'); // and remove the bad input
break;
}
processed_points.push_back(P);
}
int n = original_points.size();
for(int i=0; i<n; i++)
{
if(original_points[i] == processed_points[i])
{
cout << "Points at index " << i << " are
same\n"
<< original_points[i] << " "
<< processed_points[i] << '\n';
}
if(original_points[i] != processed_points[i])
{
cout << "Points at index " << i << " are not
same\n"
<< original_points[i] << " "
<< processed_points[i] << '\n';
}
}
return 0;
}
//end of program
Output:
Output file: mydata.txt