I am doing a homework assignment that involves writing a program to draw shapes with ASCII characters and move them across the screen. In this example, I am trying to write a method to move a circle that has already been drawn. I know my drawCircle method works, however when I try to call the drawCircle method in my moveCircle method it does not print anything out.
void CircleType::drawCircle() const{
for (int i = 0; i < NUMBER_OF_ROWS; i++) {
for(int j = 0; j < NUMBER_OF_COLUMNS; j++) {
int p = abs (x - j);
int q = abs (y - i);
int distance = pow(p, 2) + pow(q, 2);
int realDistance = pow(radius, 2);
if (abs(realDistance - distance) <= 3){ // I tested out several values here, but 3 was the integer value that consistently produced a good looking circle
drawSpace[i][j] = symbol;
}
}
}
displayShape();
return;
}
bool CircleType::moveCircle(int p, int q){
if (p - radius < 0 || p + radius > NUMBER_OF_COLUMNS){
cout << "That will move the circle off the screen"<< endl;
return false;
}
else if (q - radius < 0 || q + radius > NUMBER_OF_ROWS){
cout << "That will move the circle off the screen"<< endl;
return false;
}
else{
x = p;
y = q;
for (int m = 0; m < NUMBER_OF_ROWS; m++){
for(int n = 0; n < NUMBER_OF_COLUMNS; n++){
if (drawSpace[m][n] == symbol)
drawSpace[m][n] = ' ';
}
}
void drawCircle();
return true;
}
}
drawSpace is a 2D char array that holds the ASCII characters for the shape, and displayShape is a function that prints out that 2D array. As I said above, the drawCircle function works, however the moveCircle method does not. Am I calling the drawCircle method wrong when I try to use it in moveCircle.
That is not a function call; it is a function prototype declaration. To call the function, simply use
A function prototype simply tells the compiler that a function with a certain signature exists. This allows you do do something like this (though this is not at all common to do in this way).
If the prototype were omitted the compiler would throw an error as
Foowas not declared before its use. In a similar (and more common) vein you could also do this (called a forward declaration).Or just declare it first, but you don’t usually want a ton of functions before
main.