1,105,352 Community Members

Newbie problem

Member Avatar
Stpdoug
Light Poster
39 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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();
}
Member Avatar
bguild
Posting Whiz
339 posts since Oct 2012
Reputation Points: 163 [?]
Q&As Helped to Solve: 84 [?]
Skill Endorsements: 8 [?]
 
0
 
 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.

Member Avatar
Stpdoug
Light Poster
39 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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();
}
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article