Hey guys i have a small problem i would love some advice..this code outputs a hollowed box the problem is i cant get the right most column of the box in the right postion without tabbing it across..which will create problems if i want the user to determine how big the box should be...how can i fix this the left most column works fine when i printf when c is at 0 why doesnt the right most column do the same when c is at 9?

#include <stdio.h>
#include <conio.h>

int main()
{

int r,c;


printf("\n");

for (r=0; r<10; r++)                      // Loop runs 10 times(0 to 9)
    {   
        for (c=0; c<10; c++)
        {

            if(r==0 || r==9)              //output top and bottom of box
            {
                printf("*");
            }

                else if(c==0 )
                printf("*");              // output when c at 0 (LEFT column)

                else if(c==9)
                    printf("*");          // output when c at 9(RIGHT column)

            printf(" ");                  //output a blank space when if not true
        }

    printf("\n");                         //goes to a newline after main body of code is executed
    }


getch();
}

Recommended Answers

All 2 Replies

 printf(" "); //output a blank space when if not true

You say "if not true" in the comment but you don't actually do any test to make it happen that way. That printf will be happening every time through the loop. It is making the top and bottom of your box twice as long as they should be because it is printing a '*' and a ' ' for each column.

thanks man it was more of a rectangle before but now its a box based on input.

#include <stdio.h>
#include <conio.h>

//This program a box pattern


int main()
{

int r,c;
int bx;


printf("\nEnter size of box ");
scanf("%d",&bx);

printf("\n");

for (r=0; r<bx/2+1; r++)                       // Loop runs half of user input +1
    {   
        for (c=0; c<bx; c++)                   //Controls printing accross
        {

            if(r==0 || r==bx/2)                //output top and bottom of box
            {
                printf("*");
            }

                else if(c==0 )
                printf("*");                   // output when c at 0 (Left column)

                else if(c==bx-1)
                    printf("*");               // output (Right Column)
                else
                printf(" ");                   //output a blank space 
        }

    printf("\n");                              //goes to a newline after main loop 
    }


getch();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.