Is there a bug or my error in a custom drawRect() function? [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this questionMy drawRect() function is behaving as if there is a bug. I am trying to determine if there is a bug in the following, or, if not what I am doing incorrectly.
I am trying to make a function that displays a rectangle on the topscreen. If the programmer types in "drawRect(3,3)", a 3 by 3 rectangle is created. Yet, if the programmer types in "drawRect(3,4)", the upper right corner of the rectangle is displayed, and then an infinitely long top. Could someone help me? Here is my code:
#include <stdio.h>
#include <stdlib.h>
#define SIDES 0xB3
#define TOP_RIGHT 0xBF
#define BOTTOM_LEFT 0xC0
#define TOP_BOTTOM 0xC4
#define BOTTOM_RIGHT 0xD9
#define TOP_LEFT 0xDA
int heightloop;
int wid开发者_如何学Pythonthloop;
int displayrect(int height, int width)
{
printf("%c",TOP_LEFT);
for(widthloop=1;widthloop<width-2;width++)
{
printf("%c",TOP_BOTTOM);
}
printf("%c\n",TOP_RIGHT);
for(heightloop=1;heightloop<height-2;height++)
{
printf("%c",SIDES);
for(widthloop=1;widthloop<width-2;width++)
{
printf(" ");
}
printf("%c\n",SIDES);
}
printf("%c",BOTTOM_LEFT);
for(widthloop=1;widthloop<width-2;width++)
{
printf("%c",TOP_BOTTOM);
}
printf("%c",BOTTOM_RIGHT);
return(0);
}
In your loops you should increment widthloop
and heightloop
instead of width
and height
. Also widthloop
and heightloop
should be initialized with 0.
Loops such as this:
for(widthloop=1;widthloop<width-2;width++)
and:
for(heightloop=1;heightloop<height-2;height++)
will do nothing when width <= 3
or height <= 3
.
Also note that widthloop
and heightloop
should really be local variables, although this is not a bug per se, just a "code smell".
精彩评论