Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The file shapes.txt contains the name of a shape followed by the vertices of the

ID: 3586060 • Letter: T

Question

The file shapes.txt contains the name of a shape followed by the vertices of the shapes is shown below:

This data would correspond to a triangle with the following vertices:

And a square with the following vertices:

Note that this is a basic algorithm; it will need some refining. In reference to the last line in the outer loop, if the end of file was not reached, that would mean there is still data in the file. So we would want to reset the flags of the stream and loop back to the beginning to read in the next shape's name. If the end of file was reached, then the program is done reading from the file and should not change the state of the stream.


Your program should read from this file and print the name of the shape, followed by a colon, then the list of vertexes, each separated by a semicolon. See the example output below:

Please give answer in C++ language.

The output of your program should look exactly like this. Data values may differ.
  Triangle: (-1, 0); (0, 1); (1, 0)  Square  : (0, 0); (0, 1); (1, 1); (1, 0)  Polygon : (0, 1); (1, 5.5); (2, 2.3); (1, 0.9); (0.5, 0.1)  

Please give answer in C++ language.

Explanation / Answer

#include<iostream>

#include<fstream>

#include<vector>

#include<sstream>

using namespace std;

int main() {

    vector <int> vec;

    ifstream fin("input.txt");

    string line;

    string name;

    int i;

   

    while (getline( fin, line) ) //Read a line

    {

      stringstream ss(line);

    

      ss >> name;

    

      cout << name << " ";

   

      while(ss >> i) //Extract integers from line

        vec.push_back(i);

    

      for (int j = 0; j < vec.size(); j = j + 2) {

        if (j!=0)

           cout << "; ";

        cout << "(" << vec[j] << "," << vec[j+1] << ")";

      }

      cout << endl;

      vec.clear();

    }

fin.close();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote