Drawing a Circle with Class in C++

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.

The problem I'm having with my program is that my code compiles, however, the output goes crazy and doesn't draw the circles.

I'm still new to C++ and if anyone can help me out on how to fix this, I would appreciate it.

My Current Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
////// Circle.h
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <time.h>
#include <cmath>

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 << "Shapes deleted!" << endl;
    }

    void Circle::setType(char c)
    {
        if(c == 'S' || c == 'C')
            type = c;
    }

    void Circle::setSerialNumber(int s)
    {
        if(s > 0)
            serialNumber = s;
    }

    void Circle::setRadius(double r)
    {
        if(r > 0)
            radius = r;
    }

    void Circle::setDensity(double d)
    {
        if(d > 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 < SIZE; i++)
        if(arr[i] != nullptr)
        {
            cout << "Circle #" << arr[i]->getSerialNumber() << endl;
            drawType(arr[i]);
        }
}

void drawType(Circle *p)
{
    if(p->getType() == 'C')
        drawCircle(p);

    else if(p->getType() == 'S')
        drawSpray(p);
}

void drawCircle(Circle *p)
{
    double r = p->getRadius();
    int x = 0;
    int y = 0;
    int rto = 2;

    for(int i = 0; i <= 40; i++)
    {
        for(int j = 0; j <= 40; i++)
        {
            x = abs(i - 20);
            y = abs(j - 20);
            r = pow(pow(x, rto) + pow(y, rto), 0.5);

            if(19.5 < r && r < 20.5)
                cout << "* ";
            else
                cout << "  ";
        }

        cout << endl;
    }

}

void drawSpray(Circle *p)
{
    double d = p->getDensity();
    int x = 0;
    int y = 0;
    int rto = 2;

    for(int i = 0; i <= 80; i++)
    {
        for(int j = 0; j <= 80; i++)
        {
            x = abs(i - 30);
            y = abs(j - 30);
            d = pow(pow(x, rto) + pow(y, rto), 0.5);

            if(19.5 < d && d < 20.5)
                cout << "* ";
            else
                cout << "  ";
        }

        cout << endl;
    }
}

void deleteAll(Circle *arr[], int SIZE)
{
    for(int i = 0; i < SIZE; i++)
    {
        if(arr[i] != nullptr)
            delete arr[i];
    }
}



My Current Output:
1
2
3
Circle #1001

                       * * * * * * * * *



Expected Output: (example)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Circle #1001

                        *************
                    **                 **
                 **                       **
               *                             *
             **                               **
            *                                   *
           *                                     *
          *                                       *
          *                                       *
          *                                       *
          *                                       *
          *                                       *
          *                                       *
          *                                       *
           *                                     *
            *                                   *
             **                               **
               *                             *
                 **                       **
                    **                 **
                        *************
Read line 173 out-loud to yourself: for(int j = 0; j <= 40; i++)
And line 199, I assume that was copypasted.

By the way, since both of your functions only differ by a constant value, you could combine them and have that value as a parameter. Just a thought.
Last edited on
Topic archived. No new replies allowed.