Question: please help code this in c++ the assignment starts below the dashed line ___________________________________________________ Coordinates File Start a program to work with points, discussed in
please help code this in c++ the assignment starts below the dashed line
___________________________________________________
Coordinates File
- Start a program to work with points, discussed in 10.4. Use the files Points.h, Points.cpp, and main.cpp. Begin by defining a struct Point in Points.h that has two int coordinate membersx and y:
struct Point { int x, y; }; - Define the following operators for Point in Point.cpp, as declared in Points.h:
///Output in the format (x,y), no newline ostream& operator<<(ostream& os, const Point& p); ///Input in the form (x,y) ///if is fails (!is), return is. This is necessary for loops later on. istream& operator>>(istream& is, Point& p); ///true if the (x,y) values match bool operator==(const Point& p1, const Point& p2); bool operator!=(const Point& p1, const Point& p2); ///Output a vector of points ///Call the << operator for a Point (above) for each point in the vector ///Separate by newlines ostream& operator<<(ostream& os, const vector& points)
-
Using the code and discussion in 10.4, prompt the user in main.cpp to input seven (x,y) pairs. As the data is entered, input it using your >> operator for Points and store it in a vector of Points called original_points.
-
Print the data in original_points to see what it looks like using your << operator for a vector of Points.
-
Open an ofstream and output each point to a file named mydata.txt. On Windows, we suggest the .txt suffix to make it easier to look at the data with an ordinary text editor (such as WordPad). Use your << operator for a vector of Points, but use it with your ofstream object as the left operand:
ofs << original_points;
It will work the same way it did with cout, because they both inherit from the same class (ostream). This will be described more in a later chapter.
- Close the ofstream and then open an ifstream for mydata.txt. Read the data from mydata.txt and store it in a new vectorcalled processed_points. Use your >> operator for Points, then push_back each Point into your vector, until the ifstreamfails (use the typical while loop for file input). Use >> the same way you used it with cin, but with your ifstream object as the left operand:
Point p; while(ifs >> p) //...
-
Print the data elements from both vectors. Again, use <<.
-
Compare the two vectors and print Something's wrong! if the number of elements or the values of elements differ. Otherwise print Vectors equal!. Use the != operator to compare your two vectors. It is already defined by the std library for vectors, and it will automatically use the == operator you wrote for Points. It should only be one line to compare the vectors:
if(original_points != processed_points) //...
- Sample output:
(x,y): (0,1) (x,y): (2,3) (x,y): (4,5) (x,y): (6,7) (x,y): (8,9) (x,y): (10,11) (x,y): (12,13) (0,1) (2,3) (4,5) (6,7) (8,9) (10,11) (12,13) Data from file: (0,1) (2,3) (4,5) (6,7) (8,9) (10,11) (12,13) Original data: (0,1) (2,3) (4,5) (6,7) (8,9) (10,11) (12,13) Vectors equal!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
