Start New Discussion Reply to this Discussion Newbie problem
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();
}
Stpdoug
Light Poster
27 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.
bguild
Posting Whiz
335 posts since Oct 2012
Reputation Points: 167
Solved Threads: 79
Skill Endorsements: 8
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();
}
Stpdoug
Light Poster
27 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
© 2013 DaniWeb® LLC
Page generated in 0.2951 seconds
using 2.66MB