Thread: Box within box program

  1. #1
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43

    Box within box program

    Hello,

    Using ASCII and C programming I am creating a program whose output is a box within a box. I am very close to completion but I am stuck on one part and need your help.

    Here is the code:

    Code:
    #include <stdio.h>
    int main()
    {   
     int k;   
     char UpperLeft,UpperRight,LowerLeft,LowerRight,Vertical,Horizontal;   
     
     UpperLeft  = 218;  
      UpperRight = 191;  
      LowerLeft  = 192;  
      LowerRight = 217;   
     Vertical   = 179;    
    Horizontal = 196;       
     
     printf("\n\n\n\n\n\n\n\n\n\n\t    ");
    
            for ( k = 0; k < 40; k++)
        {        printf("%c",Horizontal);    
                printf("%c",UpperRight);
    }
              
             for ( k = 0; k < 3; k++)
        {        printf("\n\t\t  %c",Vertical);    
                 printf("\t\t\t\t          %c",Vertical);    }    
    
    printf("\n\t\t   ");  
      
    printf("%c",UpperLeft);  
     
     for ( k = 0; k < 36; k++)
        {        printf("%c",Horizontal);    }    
    
    printf("%c",UpperRight); 
    
           
            for ( k = 0; k < 10; k++)
        {        printf("\n\t\t    %c",Vertical);     
    
       printf("\t\t\t\t         %c",Vertical);    }    
    
    printf("\n                  ");  
     
     printf("%c", LowerLeft);  
     
     for ( k = 0; k < 36; k++)
        {        printf("%c",Horizontal);    }   
    
     printf("%c", LowerRight);   
    
     printf("\n\n\n\n\n\n\n");
    
    Which gives me:

    Box within box program-output1-png


    As I increase the size of the outer box, the lower one keeps sliding down lines becuase the \n is included with the "for loop". Is there a way to prevent this so I can increase the outer without affecting the inner box? Thanks.
    Last edited by jamesallen4u; 10-23-2011 at 07:58 PM. Reason: Ugly Formatting

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Are you allowed to use arrays?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I wouldn't do this with arrays.

    Your code is too hard to follow because you have magic numbers all over the place. If something depends on a number, give that number a name and #define it as a constant.

    So I'll give you general advice: stop coding, and think. Figure out how your program should work. How do you display intersecting boxes?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by King Mir View Post
    I wouldn't do this with arrays.
    Why?

    Define an array as big as your screen. Draw the box in the array. Print the array.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Well it sort of depends on how generic you want to be. If you want to have a single function that draws one box, then you need an array. But if you only ever need two boxes, then you can just print them directly.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43
    Thanks for the suggestions. In the end, I just printed out line by line instead of using arrays.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Shouldn't that ghost be chasing Pac Man, since it's not dark blue?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43
    Quote Originally Posted by quzah View Post
    Shouldn't that ghost be chasing Pac Man, since it's not dark blue?


    Quzah.

    In a perfect world, you would be correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM