My 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 widthloop;
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
widthloopandheightloopinstead ofwidthandheight. Alsowidthloopandheightloopshould be initialized with 0.