In: Computer Science
#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
}
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();
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
}
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;
}
Depend on this code, if the user enters the wrong input like
(8.5,8.7) change in to (8,8) then let the user enter another input
again the user enter like (er,yu) ignore this input and let the
user enter another input the user wants to terminate use file
termination. is there anybody can help
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
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)
float xf,yf;
in >> d1 >> xf >>
d2 >> yf >> d3;
P.x = floor(xf);
P.y = floor(yf);
return in;
}
};
int main()
{
vector<Point> original_points;
cout << "Enter points :\n";
Point P;
// loop continues until any error occurs
while(!cin.eof())
{
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
}else{
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();
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
}
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;
}