i am suppose to write a program that calculates measurements of shapes inputed from a txt file. i did that and wrote arrays for each measurement in a struct, but i am kind of confused on what i am supposed to change in my program, my professor couldn't explain it clear enough. i guess i am suppose to have one dynamic array for all shapes and its measurements instead of having an array of doubles for each measurement of the shape.
how would i go about doing this because i am not to sure.
i cut the code down because it's really lengthy, and i only put in one shape (rectangle) so it will be easier to read.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::ios;
#include <iomanip>
using std::setprecision;
#include <fstream>
using std::ifstream;
#include <cmath>
#include <cstring>
using std::strtok;
using std::strcmp;
#include <cstdlib>
constint MAX_CHARS_PER_LINE = 50;
constint MAX_TOKENS_PER_LINE = 4;
constchar* DELIMITER = " ";
// structs of of shapes with array of the required information for those shapes
struct Rectangle
{
double length[100];
double width[100];
};
// void functions that allow for calculation of the shapes measurements
void rectangleCalc(double, double);
int main()
{
//measurements come from an input file
ifstream fin;
fin.open("geo.txt");
if(!fin.good())
return 1;
// sets up a char array
char* token[MAX_TOKENS_PER_LINE] = {0};
//sets a variable to the struct shape, allowing it to be accessed
Rectangle shapeR;
// counter of all valid shapes in text file
int validRectangles = 0;
// while statement that tokenizes the txt file, and also validates if the shape is valid
while(!fin.eof())
{
char buf[MAX_CHARS_PER_LINE];
fin.getline(buf, MAX_CHARS_PER_LINE);
int n = 0;
token[0] = strtok(buf, DELIMITER);
if(token[0])
{
for(n = 1; n < MAX_TOKENS_PER_LINE; n++)
{
token[n] = strtok(0, DELIMITER);
if(!token[n]) break;
} //for
if((strcmp(token[0], "RECTANGLE") == 0))
{
if(n != 3)
cout << token[0] << " Invalid Object" << endl;
else
{
shapeR.length[validRectangles] = atof(token[1]);
shapeR.width[validRectangles] = atof(token[2]);
validRectangles++;
} //else
} //if
} //while
cout << endl;
// calculates the measurements and then prints out the measurements of each valid shape
for(int i = 0; i < validRectangles; i++)
rectangleCalc(shapeR.length[i], shapeR.width[i]);
cout << endl;
cout << "Press Enter to continue..." << endl;
cin.get();
return 0;
} //main
// void functions with calculation and print out statements for each shape
void rectangleCalc(double side1, double side2)
{
double perimeter;
double area;
perimeter = (side1 * 2) + (side2 * 2);
area = side1 * side2;
cout.setf(ios::fixed|ios::showpoint);
cout << setprecision(1);
cout << "RECTANGLE side1=" << side1 << " side2=" << side2;
cout << " perimeter=" << perimeter << " area=" << area;
cout << endl;
} //rectangleCalc