title: Drawing a Circle with Class in C++
body: <p>I'm currently writing a program where I have to draw three circle shapes from a class(data structure) that will output in the console. </p>
  
  <p>The problem I'm having with my program is that my code compiles, however, the output goes crazy and doesn't draw the circles. </p>
  
  <p>I'm still new to C++ and if anyone can help me out on how to fix this, I would appreciate it. </p>
  
  <p>My Current Code:</p>
  
  <pre><code>////// Circle.h
  #include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sstream&gt;
  #include &lt;time.h&gt;
  #include &lt;cmath&gt;
  
  using namespace std;
  
  class Circle
  {
  private:
      char type;
      int serialNumber = 0;
      double radius = 0.0;
      double density = 0.0;
  
  public:
      Circle(char, int, double);
      Circle(char, int, double, double);
      ~Circle();
  
      void setType(char);
      void setSerialNumber(int);
      void setRadius(double);
      void setDensity(double);
  
      char getType() const;
      int getSerialNumber() const;
      double getRadius() const;
      double getDensity() const;
  };
  
  ////// Circle.cpp
  // #include "Circle.h"
  
      Circle::Circle(char c, int s, double r)
      {
          type = c;
          serialNumber = s;
          radius = r;
      }
  
      Circle::Circle(char c, int s, double r, double d)
      {
          type = c;
          serialNumber = s;
          radius = r;
          density = d;
      }
  
      Circle::~Circle()
      {
          cout &lt;&lt; "Shapes deleted!" &lt;&lt; endl;
      }
  
      void Circle::setType(char c)
      {
          if(c == 'S' || c == 'C')
              type = c;
      }
  
      void Circle::setSerialNumber(int s)
      {
          if(s &gt; 0)
              serialNumber = s;
      }
  
      void Circle::setRadius(double r)
      {
          if(r &gt; 0)
              radius = r;
      }
  
      void Circle::setDensity(double d)
      {
          if(d &gt; 0)
              density = d;
      }
  
      char Circle::getType() const
      {
          return type;
      }
  
      int Circle::getSerialNumber() const
      {
          return serialNumber;
      }
  
      double Circle::getRadius() const
      {
          return radius;
      }
  
      double Circle::getDensity() const
      {
          return density;
      }
  
  ////// main.cpp
  // #include "Circle.h"
  
  void drawAll(Circle *[], int);
  void drawType(Circle *);
  void drawCircle(Circle *);
  void drawSpray(Circle *);
  void deleteAll(Circle *[], int);
  
  /// For 'C' type the system should display a circle.
  /// For 'S' type the system displays a spray pattern just like those used in Microsoft Paint.
  
  int main(int argc, char** argv)
  {
      const int SIZE = 3;
      Circle * arrCircle[SIZE] = {nullptr};
  
      arrCircle[0] = new Circle('C', 1001, 20);
  
      /// Create a Circle whose serial number is 1001 and the radius is 20.
  
      /// Type 'C' indicates Circle type.
  
      arrCircle[1] = new Circle('S', 1002, 25, 30);
  
      /// Create a Spray whose serial number is 1002, the radius is 25, and the density is 30%.
  
      /// Type 'S' indicates Spray type.
  
      arrCircle[2] = new Circle('S', 1003, 40, 80);
  
      /// Create a Spray whose serial number is 1003, the radius is 40, and the density is 80%.
  
      drawAll(arrCircle, SIZE);
  
      /// Draw all shapes. The function uses a for loop to display the circles and sprays in arrCircle.
  
      deleteAll(arrCircle, SIZE);
  
      /// Delete all shapes.
  
      return 0;
  }
  
  void drawAll(Circle *arr[], int SIZE)
  {
      for(int i = 0; i &lt; SIZE; i++)
          if(arr[i] != nullptr)
          {
              cout &lt;&lt; "Circle #" &lt;&lt; arr[i]-&gt;getSerialNumber() &lt;&lt; endl;
              drawType(arr[i]);
          }
  }
  
  void drawType(Circle *p)
  {
      if(p-&gt;getType() == 'C')
          drawCircle(p);
  
      else if(p-&gt;getType() == 'S')
          drawSpray(p);
  }
  
  void drawCircle(Circle *p)
  {
      double r = p-&gt;getRadius();
      int x = 0;
      int y = 0;
      int rto = 2;
  
      for(int i = 0; i &lt;= 40; i++)
      {
          for(int j = 0; j &lt;= 40; i++)
          {
              x = abs(i - 20);
              y = abs(j - 20);
              r = pow(pow(x, rto) + pow(y, rto), 0.5);
  
              if(19.5 &lt; r &amp;&amp; r &lt; 20.5)
                  cout &lt;&lt; "* ";
              else
                  cout &lt;&lt; "  ";
          }
  
          cout &lt;&lt; endl;
      }
  
  }
  
  void drawSpray(Circle *p)
  {
      double d = p-&gt;getDensity();
      int x = 0;
      int y = 0;
      int rto = 2;
  
      for(int i = 0; i &lt;= 80; i++)
      {
          for(int j = 0; j &lt;= 80; i++)
          {
              x = abs(i - 30);
              y = abs(j - 30);
              d = pow(pow(x, rto) + pow(y, rto), 0.5);
  
              if(19.5 &lt; d &amp;&amp; d &lt; 20.5)
                  cout &lt;&lt; "* ";
              else
                  cout &lt;&lt; "  ";
          }
  
          cout &lt;&lt; endl;
      }
  }
  
  void deleteAll(Circle *arr[], int SIZE)
  {
      for(int i = 0; i &lt; SIZE; i++)
      {
          if(arr[i] != nullptr)
              delete arr[i];
      }
  }
  
  </code></pre>
  
  <p>My Current Output:</p>
  
  <pre><code>Circle #1001
  
                         * * * * * * * * *
  </code></pre>
  
  <p>Expected Output: (example)</p>
  
  <pre><code>Circle #1001
  
                          *************
                      **                 **
                   **                       **
                 *                             *
               **                               **
              *                                   *
             *                                     *
            *                                       *
            *                                       *
            *                                       *
            *                                       *
            *                                       *
            *                                       *
            *                                       *
             *                                     *
              *                                   *
               **                               **
                 *                             *
                   **                       **
                      **                 **
                          *************
  
  Circle #1002
  
                                ***************
                           ***                   ***
                        **                           **
                      *                                 *
                    *                                     *
                  **                                       **
                 *                                           *
                *                                             *
               *                                               *
              **                                               **
              *                                                 *
              *                                                 *
              *                                                 *
              *                                                 *
              *                                                 *
              *                                                 *
              **                                               **
               *                                               *
                *                                             *
                 *                                           *
                  **                                       **
                    *                                     *
                      *                                 *
                        **                           **
                           ***                   ***
                                ***************
  
  Circle #1003
  
                                                      *****************                                                   
                                               ***                         ***                                            
                                           **                                   **                                        
                                        *                                           *                                     
                                     **                                               **                                  
                                   *                                                     *                                
                                 *                                                         *                              
                               *                                                             *                            
                             **                                                               **                          
                            *                                                                   *                         
                           *                                                                     *                        
                          *                                                                       *                       
                         *                                                                         *                      
                        *                                                                           *                     
                       *                                                                             *                    
                       *                                                                             *                    
                      *                                                                               *                   
                      *                                                                               *                   
                      *                                                                               *                   
                      *                                                                               *                   
                      *                                                                               *                   
                      *                                                                               *                   
                      *                                                                               *                   
                      *                                                                               *                   
                      *                                                                               *                   
                       *                                                                             *                    
                       *                                                                             *                    
                        *                                                                           *                     
                         *                                                                         *                      
                          *                                                                       *                       
                           *                                                                     *                        
                            *                                                                   *                         
                             **                                                               **                          
                               *                                                             *                            
                                 *                                                         *                              
                                   *                                                     *                                
                                     **                                               **                                  
                                        *                                           *                                     
                                           **                                   **                                        
                                               ***                         ***                                            
                                                      ***************** 
  </code></pre>