okay i came up with some code that reads in a file, and on the file are names of shapes and their widths/lengths. after the file is read in, it tokenizes whats in the file, it then calculates what shape it is and then takes the information and calculates the area, perimeter, volume, and etc of the shape.

the program does everything i need it to do, but when the input file has a shape with insufficient information to calculate its lengths, the program stops where the shape has insufficient info and crashes.

i need it to see if the shape doesn't have enough information to calculate, and if it doesn't it will output the name of the shape and that it is invalid object.

any help would be greatful.

here is my code that i came up with:

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <fstream>
using std::ifstream;

#include <cmath>

#include <cstring>
using std::strtok;
using std::strcmp;

#include <cstdlib>

const int MAX_CHARS_PER_LINE = 50;
const int MAX_TOKENS_PER_LINE = 4;
const char* DELIMITER = " ";

void squareCalc(double);
void rectangleCalc(double, double);
void circleCalc(double);
void cubeCalc(double);
void prismCalc(double, double, double);
void sphereCalc(double);
void cylinderCalc(double);
void cylinderCalc(double, double);
void triangleCalc(double, double, double);
void invalidObj(int);

int main()
{
  cout << endl;
  cout << "Description: Calculates area, perimeter, surface area, and volume" << endl;
  cout << "of 6 different geometric objects." << endl;
  cout << endl;
  
  ifstream fin;
  fin.open("geo.txt");
  if(!fin.good())
    return 1;

  char* token[MAX_TOKENS_PER_LINE] = {0};

  double squareSide = 0;
  double rectangleSide1 = 0;
  double rectangleSide2 = 0;
  double circle = 0;
  double cubeSide = 0;
  double prismSide1 = 0;
  double prismSide2 = 0;
  double prismSide3 = 0;
  double sphere = 0;
  double cylinder1 = 0;
  double cylinder2 = 0;
  double triangleSide1 = 0;
  double triangleSide2 = 0;
  double triangleSide3 = 0;

  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], "SQUARE") == 0))
      {
        squareSide = atof(token[1]);
        squareCalc(squareSide);
      } //if
    
      if((strcmp(token[0], "CIRCLE") == 0))
      {
        circle = atof(token[1]);
        circleCalc(circle);
      } //if
    
      if((strcmp(token[0], "CUBE") == 0))
      {
        cubeSide = atof(token[1]);
        cubeCalc(cubeSide);
      } //if
      
      if((strcmp(token[0], "SPHERES") == 0))
      {
        sphere = atof(token[1]);
        sphereCalc(sphere);
      } //if
      
      if((strcmp(token[0], "RECTANGLE") == 0))
      {
        rectangleSide1 = atof(token[1]);
        rectangleSide2 = atof(token[2]);
        rectangleCalc(rectangleSide1, rectangleSide2);
      } //if
      
      if((strcmp(token[0], "PRISM") == 0))
      {
        prismSide1 = atof(token[1]);
        prismSide2 = atof(token[2]);
        prismSide3 = atof(token[3]);
        prismCalc(prismSide1, prismSide2, prismSide3);
      } //if

      if((strcmp(token[0], "CYLINDER") == 0))
      {
        cylinder1 = atof(token[1]);
        cylinder2 = atof(token[2]);
        cylinderCalc(cylinder1, cylinder2);
      } //if

      if((strcmp(token[0], "TRIANGLE") == 0))
      {
        triangleSide1 = atof(token[1]);
        triangleSide2 = atof(token[2]);
        triangleSide3 = atof(token[3]);
        triangleCalc(triangleSide1, triangleSide2, triangleSide3);
      } //if
    } //if

    cout << endl;
  } //while

  cout << endl;
  cout << "Press Enter to continue..." << endl;
  cin.get();

  return 0;
} //main

void squareCalc(double side)
{
  double perimeter;
  double area;

  perimeter = side * 4;
  area = side * side;

  cout << "SQUARE side=" << side << " perimeter=" << perimeter;
  cout << " area=" << area;
} //squareCalc

void rectangleCalc(double side1, double side2)
{
  double perimeter;
  double area;

  perimeter = (side1 * 2) + (side2 * 2);
  area = side1 * side2;

  cout << "RECTANGLE side1=" << side1 << " side2=" << side2;
  cout << " perimeter=" << perimeter << " area=" << area;
} //rectangleCalc

void circleCalc(double side1)
{
  double circumference;
  double area;
  double pow(double, int);

  double pie = 3.14;

  circumference = 2 * pie * side1;
  area = pie * pow(side1, 2);

  cout << "CIRCLE radius=" << side1;
  cout << " circumference=" << circumference << " area=" << area;
} //rectangleCalc

void cubeCalc(double side1)
{
  double volume;
  double surfaceArea;

  double pow(double, int);

  volume = pow(side1, 3);
  surfaceArea = 6 * pow(side1, 2);

  cout << "CUBE side=" << side1;
  cout << " volume=" << volume << " surface area=" << surfaceArea;
} //rectangleCalc

void prismCalc(double side1, double side2, double side3)
{
  double volume;
  double surfaceArea;

  volume = side1 * side2 * side3;
  
  double lw = side1 * side2;
  double lw2 = 2 * side1 + 2 * side2;
  
  surfaceArea = 2 * lw + lw2 * side3;

  cout << "PRISM side1=" << side1 << " side2=" << side2 << " side3=" << side3;
  cout << " volume=" << volume << " surface area=" << surfaceArea;
} //prismCalc

void sphereCalc(double side1)
{
  double volume;
  double surfaceArea;

  double pie = 3.14;

  volume = (4/3) * pie * pow(side1, 3);
  surfaceArea = 4 * pie * pow(side1, 2);

  cout << "SPHERE radius=" << side1;
  cout << " volume=" << volume << " surfaceArea=" << surfaceArea;
} //rectangleCalc

void cylinderCalc(double side1, double side2)
{
  double volume;
  double surfaceArea;

  double pie = 3.14;

  volume = 2 * pie * pow(side1, 2) * side2;
  surfaceArea = 2 * pie * pow(side1, 2) + 2 * pie * side1 * side2;

  cout << "CYLINDER side1=" << side1 << " side2=" << side2;
  cout << " volume=" << volume << " surfaceArea=" << surfaceArea;
} //rectangleCalc

void triangleCalc(double side1, double side2, double side3)
{
  double perimeter;
  double area;
  float half = 1 / 2;

  perimeter = side1 + side2 + side3; 
  area = half * side1 * side2;

  cout << "TRIANGLE side1=" << side1 << " side2=" << side2 << " side3=" << side3;
  cout << " perimeter=" << perimeter << " area=" << area;
} //triangleCalc

here is the WORKING input file:

SQUARE 14.5
RECTANGLE 14.5    4.65
CIRCLE 14.5
CUBE 13
PRISM 1 2 3

SPHERES 2.4
CYLINDER 50 1.23
TRIANGLE 1.2 3.2 3.4
SQUARE 2.4

here is the NON-WORKING input file (CYLINDER is added to the end with not enough info):

SQUARE 14.5
RECTANGLE 14.5    4.65
CIRCLE 14.5
CUBE 13
PRISM 1 2 3

SPHERES 2.4
CYLINDER 50 1.23
TRIANGLE 1.2 3.2 3.4
SQUARE 2.4
CYLINDER 5

You might be able to do something like this.

//...
      if((strcmp(token[0], "PRISM") == 0))
      {
        if((prismSide1 = atof(token[1]) = 0.0);
            run error code
        if((prismSide2 = atof(token[2]) = 0.0);
            run error code
        if((prismSide3 = atof(token[3]) = 0.0);
            run error code
        prismCalc(prismSide1, prismSide2, prismSide3);
      } //if
//...

Personally I would use string streams for the conversion and then check if the stringstream failed or not.

//...
  if((strcmp(token[0], "PRISM") == 0))
  {
    if((prismSide1 = atof(token[1]) = 0.0);
        run error code
    if((prismSide2 = atof(token[2]) = 0.0);
        run error code
    if((prismSide3 = atof(token[3]) = 0.0);
        run error code
    prismCalc(prismSide1, prismSide2, prismSide3);
  } //if
//...

okay i tried that but it did not work. now it crashes the program instantly.

the part that says run error code is for you to complete. You need to put the code there to handle an invalid object.

and if the shape doesn't have enough information then there won't be anything in token[2] or token[3]. so when i put the code for if((prismSide2 = atof(token[2]) = 0.0); there is nothing in token[2] so i think that is what is making the program crash. how would i go about by fixing this

oh and yeah i know. i did fill that part in with my own code. that worked fine with the shapes that had enough info to calculate the perimeter, etc... its the invalid objects with tokens that are empty.

for instance
if(cylinderSide1 = atof(token[1]) = 0);
cout << "Invalid Object";
if(cylinderSide2 = atof(token[2]) = 0);
cout << "Invalid Object";

for calculating the perimeter and area of a cylinder you need 2 values, so in turn this function would take 2 tokens. the first token being a positive value and the second token being nothing. this is where the program crashs for me. when token[2] is empty it crashs

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.