2011-08-01 49 views
0

我的drawRect()函數表現得好像有一個錯誤。我試圖確定是否在下面有一個錯誤,或者,如果不是我做錯了。在自定義drawRect()函數中有錯誤還是我的錯誤?

我想做一個功能,在屏幕上顯示一個矩形。如果程序員輸入「drawRect(3,3)」,則創建一個3乘3的矩形。然而,如果程序員輸入「drawRect(3,4)」,則顯示矩形的右上角,然後顯示無限長的頂部。有人能幫助我嗎?這裏是我的代碼:

#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); 
} 

回答

2

在你的循環中,你應該增加widthloopheightloop而不是widthheight。另外widthloopheightloop應該用0初始化。

+0

謝謝!我想我正在增加矩形的寬度,因此矩形的寬度不斷增加,就像我遇到的問題一樣。 – smilinggoomba

0

循環像這樣:

for(widthloop=1;widthloop<width-2;width++) 

和:

​​

會一籌莫展的時候,width <= 3height <= 3

還要注意的是widthloopheightloop確實應該局部變量,雖然這不是一個錯誤本身,只是一個「代碼味道」。