I'm having trouble making this program with linked lists work. Where I am having a problem is in printarea()

//****************************************************************
//  This program tests creating dynamic linked list of
//  several Dynamic (descendents)
//  It uses:  shape1.h  header file
//  Filename: prtshape.cpp
//****************************************************************
#include <iomanip>
#include <iostream>
#include "shape1.h"

using namespace std;

struct Shapelink
{
  char type;
  Shape *item;
  Shapelink *next;
};

class Dynamic
{
  protected:
    Shapelink *lastone;
    void linkup (Shape *, char);
  public:
    Dynamic (void);
    void createspace (int);
    void printarea (void);
    int menu(void);
};

//-------------------------- Dynamic methods ----------------------------                     

Dynamic::Dynamic()
{
  lastone = NULL;
}

void
Dynamic::linkup(Shape *newshape, char type)
{
  Shapelink *another;
 
  another = new Shapelink;
  another->type = type;
  another->item = newshape;
  another->next = lastone;
  lastone = another;
}

void
Dynamic::createspace (int type)
{
  Circle *cir;
  Rectangle *rect;
  Triangle *tri;
  Square *sq;
  float x, y, z;
 
  switch (type)
  {
    case 1:
         cir = new Circle;
         cout << "Enter the circle's radius ";
         cin  >> x;
         cir->store_radius(x);
         linkup(cir,'c');
         break;
    case 2:
         rect = new Rectangle;
         cout << "Enter the rectangle's length:" << endl;
         cin >> x;
         cout << "Enter the retangle's width:" << endl;
         cin >> y;
         rect->store_sides(x,y);
         linkup(rect,'r');
         break;
    case 3:
         tri = new Triangle;
         cout << "Enter the triangle's three sides:" << endl;
         cin >> x >> y >> z;
         tri->store_sides(x,y,z);
         linkup(tri,'t');
         break;
    case 4:
         sq = new Square;
         cout << "Enter the square's side:" << endl;
         cin >> x;
         sq->store_side(x);
         linkup(sq,'t');
         break;
    case 5:
         printarea();
         break;
    case 6:
         system("exit");
    default:
         cout << "Error: wrong choice!" << endl;
         break;
  }
}

int
Dynamic::menu()
{
  int x;
  cout << "Choose one:" << endl;
  cout << "1: Circle" << endl;
  cout << "2: Rectangle" << endl;
  cout << "3: Triangle" << endl;
  cout << "4: Square" << endl;
  //cout << "5: Print area's" << endl;
  cout << "6: Exit" << endl;
  cin >> x;
  return x;
}

void
Dynamic::printarea()
{
   Dynamic start;
   Shape *local = new Shape;
   Shapelink *shape;
   shape = lastone;
  
   while(lastone != NULL)
   {
     shape = lastone;
     lastone = lastone->next;
     shape->local.calc_area();         
   }               
}

 

 


//-------------------------- normal functions --------------------------
 

//-------------------------- main() ----------------------------

int
main()
{
  Dynamic start;
  int choice;
 
  choice = start.menu();
  while (choice >= 1 && choice <= 4)
  {
    start.createspace(choice);
    choice = start.menu();
  }
 
 
  start.printarea();
 
  system("pause");
  return 0;
 
}

Here is the class that does all of the calculations:

//******************************************************************
//  This is a header file with a class hierarchy as follows
//                       Shape
//       Circle        Rectangle        Triangle
//  Cylinder  Sphere    Square
//
//  Filename: shape1.h
//******************************************************************

#include <cmath>
#include <iostream>

using namespace std;

class Shape
{
  public:
    float return_area ();
    float return_perimeter ();
    void  print_area();
    void  print_perimeter();
    virtual float calc_area ();
    virtual float calc_perimeter ();
};

class Circle : public Shape
{
  protected:
    float radius;
  public:
    void store_radius(float);
    float return_radius ();
    virtual float calc_perimeter(void);
    virtual float calc_area(void);
};

class Rectangle : public Shape
{
  protected:
    float length, width;
  public:
    void store_sides (float, float);
    void return_sides (float &, float &);
    virtual float calc_area ();
    virtual float calc_perimeter ();
};

class Triangle : public Shape
{
  protected:
    float side1, side2, side3;
  public:
    void store_sides (float, float, float);
    void return_sides (float &, float &, float &);
    virtual float calc_area ();
    virtual float calc_perimeter ();
};

class Cylinder : public Circle
{
  protected:
    float length;
  public:
    void store_length(float);
    float return_length();
    virtual float calc_area ();
    float calc_volume ();
};

class Sphere : public Circle
{
  public:
    virtual float calc_area ();
    float calc_volume ();
};

class Square : public Rectangle
{
  public:
    void store_side (float);
    float return_side (void);
};

//------------------ Shape's methods --------------------

float
Shape::return_area ()
{
  return calc_area();
}

float
Shape::return_perimeter ()
{
  return calc_perimeter();
}

void
Shape::print_area()
{
  cout << "The area is : " << calc_area() << endl;
}

void
Shape::print_perimeter()
{
  cout << "The perimeter is : " << calc_perimeter() << endl;
}

float
Shape::calc_area ()
{
  return -1;
}

float
Shape::calc_perimeter ()
{
  return -1;
}

//----------------- Circle's methods --------------------

void
Circle::store_radius(float value)
{
  radius = value;
}

float
Circle::return_radius()
{
  return radius;
}


float
Circle::calc_perimeter(void)
{
  float circum;
  circum = 3.14*2*radius;
  return circum;
}

float
Circle::calc_area(void)
{
  return (3.14*radius*radius);
}

//----------------- Rectangle's methods -----------------

void
Rectangle::store_sides(float l, float w)
{
  length = l;
  width = w;
}

void
Rectangle::return_sides(float &l, float &w)
{
  l = length;
  w = width;
}


float
Rectangle::calc_perimeter(void)
{
  return (2 * length + 2 * width);
}

float
Rectangle::calc_area(void)
{
  return (length * width);
}

//----------------- Triangle's methods ------------------

void
Triangle::store_sides(float s1, float s2, float s3)
{
  side1 = s1;
  side2 = s2;
  side3 = s3;
}

void
Triangle::return_sides(float &s1, float &s2, float &s3)
{
  s1 = side1;
  s2 = side2;
  s3 = side3;
}


float
Triangle::calc_perimeter(void)
{
  return (side1 + side2 + side3);
}

float
Triangle::calc_area(void)
{
  float s, area;
 
  s = (side1 + side2 + side3)/2;
  area = sqrt(s * (s - side1) * (s - side2) * (s - side3) );
  return area;
}

//----------------- Cylinder's methods ------------------

void
Cylinder::store_length(float l)
{
  length = l;
}

float
Cylinder::return_length()
{
  return length;
}

float Cylinder::calc_area ()
{
  float base, side;
 
  base = Circle::calc_area();
  side = length * calc_perimeter();
  return (2 * base + side);
}

float Cylinder::calc_volume ()
{
  return (Circle::calc_area() * length);
}

//----------------- Sphere's methods --------------------

float
Sphere::calc_area ()
{
  return (4.0 * 3.14 * radius * radius);
}

float
Sphere::calc_volume ()
{
  return (4.0 / 3.0 * 3.14 * radius * radius * radius);
}

//----------------- Square's methods --------------------

void
Square::store_side (float side)
{
  length = side;
  width  = side;
}

float
Square::return_side (void)
{
  return length;
}

Recommended Answers

All 2 Replies

You have invented a rather strange one-time list where new members are inserted in the head and the first traversal is at the same time the last possible one. After the 1st call of (incorrect) printarea the list becomes unusable. It losts all elements.

Well, non-destructive Dynamic::printarea looks like

void Dynamic::printarea()
{
    for (Shapelink* plink = lastone; 
         plink != 0; 
         plink = plink->next) {
        plink->item->print_area();
    }
}

There are lots of other defects in your code but it's another story ;)...

The prof wrote most of..well pretty much all of the code. We just had to write the printarea() function
Thanks, I'll try what you said!

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.