Question: I currently writing a C++ program that calculates area, perimeter, surface area, and volume of common 2D and 3D shapes. Then, the program outputs the

I currently writing a C++ program that calculates area, perimeter, surface area, and volume of common 2D and 3D shapes. Then, the program outputs the results on the console and in a separate text file. The program takes input from a text file called Shapes.input.txt.

Input file:

SQUARE 14.5 344

SQUARE

RECTANGLE 14.5 4.65

DIMENSIONS

CIRCLE 14.5

BOX x 2 9

CUBE 13

BOX 1 2 3

CYLINDER 2.3 4 56

CANDY

SPHERE 2.4

CYLINDER 1.23

CYLINDER 50 1.23

TRIANGLE 1.2 3.2

PRISM 2.199 5

EOF

I already have the completed program with the desired output but it required to have a parsing function that I had trouble figuring out how to implement. Any help I get will be well appreciated.

Parsing function required to be used in the program:

Expected Output:

My Code:

#include  #include  #include  #include  #include  #include  #define PI 3.14159 using namespace std; int main() { ifstream in("Shapes.input.txt"); ofstream out("Shapes.output.txt"); stringstream sstr; map < string, int > shapes; string shape, line; float length, width, height, area, peri, volume; //variable declaration shapes.insert(pair("SQUARE", 1)); shapes.insert(pair("RECTANGLE", 2)); shapes.insert(pair("CIRCLE", 3)); shapes.insert(pair("TRIANGLE", 4)); shapes.insert(pair("CUBE", 5)); shapes.insert(pair("BOX", 6)); shapes.insert(pair("CYLINDER", 7)); shapes.insert(pair("PRISM", 8)); if (!in || !out) { cout << "Unable to open file(s)"; exit(1); } cout.precision(2); cout << fixed; out.precision(2); out << fixed; while (getline(in, line)) { length = width = height = 0; sstr << line; if (sstr >> shape) { switch(shapes[shape]) { case 1: //Square sstr >> length; area = length * length; peri = 4.0 * length; cout << shape << " side = " << length << " area = " << area << " perimeter=" << peri << endl; out << shape << " side = " << length << " area = " << area << " perimeter = " << peri << endl; break; case 2: //Rectangle sstr >> length >> width; area = (length * width); peri = 2.0 * (length + width); cout << shape << " length = " << length << " width = " << width << " area = " << area << " perimeter = " << peri << endl; out << shape << " length=" << length << " width = " << width << " area = " << area << " perimeter = " << peri << endl; break; case 3: //Circle sstr >> length; area = PI * (length * length); peri = 2 * PI * length; cout << shape << " radius = " << length << " area = " << area << " perimeter = " << peri << endl; out << shape << " radius = " << length << " area = " << area << " perimeter = " << peri << endl; break; case 4: //Triangle sstr >> length; area = (1.732 * length * length) / 4.0; peri = 3.0 * length; cout << shape << " side = " << length << " area = " << area << " perimeter = " << peri << endl; out << shape << " side = " << length << " area = " << area << " perimeter = " << peri << endl; break; case 5: //Cube sstr >> length; area = 6 * length * length; volume = length * length * length; cout << shape << " side = " << length << " surface area = " << area << " volume = " << volume << endl; out << shape << " side = " << length << " surface area = " << area << " volume = " << volume << endl; break; case 6: //BOX sstr >> length >> width >> height; area = 2.0 * (length * width + length * height + width * height); volume = length * width * height; cout << shape << " length = " << length << " width = " << width << " height = " << height << " surface area = " << area << " volume = " << volume << endl; out << shape << " length = " << length << " width = " << width << " height = " << height << " surface area = " << area << " volume = " << volume << endl; break; case 7: //Cylinder sstr >> length >> height; area = 2.0 * PI * length * (length + height); volume = PI * length * length * height; cout << shape << " radius = " << length << " height = " << height << " surface area = " << area << " volume = " << volume << endl; out << shape << " radius = " << length << " height = " << height << " surface area = " << area << " volume = " << volume << endl; break; case 8: //Prism sstr >> length >> height; area = 3.0 * (length * height) + (1.732 * length * length) / 2.0; volume = (1.732 * length * length * height) / 4.0; cout << shape << " side = " << length << " height = " << height << " surface area = " << area << " volume = " << volume << endl; out << shape << " side = " << length << " height = " << height << " surface area = " << area << " volume = " << volume << endl; break; default: cout << shape << " invalid object" << endl; } } sstr.str(string()); sstr.clear(); } in.close(); out.close(); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!