Thread: Box program almost completed need additional help

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    Box program almost completed need additional help

    Hello everyone, I am almost finished writing a box program based on the users input of the size of the box.

    So far my program is
    Code:
    #include <stdio.h>
    
    int
    main(void)
    {
    int num;
    int top;
    int newtop;
    int oside;
    int obot;
    int bot;
    int side;
    
            printf("Enter the size of a box: "); /*takes input from user*/
            scanf("%d", &num);
    top = 0;
    side = 0;
    bot = 0;
    newtop = 0;
    oside = 0;
    obot = 0;
    
    while (newtop != num) {
    newtop++ ;
    printf("*");
    
    }
    while (side != num) {
    side++ ;
    printf("*\n");
    
    }
    while(bot != num) {
    bot++ ;
    printf("*");
    
    }
    
    return 0;
    }
    A sample run of the program :
    Code:
    linux3[3]% ./a.out
    Enter the size of a box: 5
    ******
    *
    *
    *
    *
    *****
    linux3[4]%
    However to complete the project how would I go about printing a box like this

    http://tinyurl.com/3kr3unl

    Any help would be appreciated ! Thank you in advance
    Last edited by Rukris; 10-31-2011 at 08:20 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's best to print both sides of the box, at the same time, so change this:
    Code:
    while (side != num) {
       side++ ;
       printf("*\n");
    
    }
    sides2print = heightoftheBox-2;
    
    //to something like this:
    while(side!= sides2print) {
       if(side==0 || side equals num) 
          print *
       else
           print space ' ' char
        if(side equals widthOfTheBox) {
           print newline
           side++;
        }
    }
    If the user enter 3 x 3 box, the side loop will print one "row" only, since the other two rows are either the top or the bottom. So it's total height of the box, -2 equals side rows to print.

    You increment the side variable, only when it has printed the * on the far right hand side of the box, and it has printed the newline.

    Get that going and I'll help you with the diagonals. The above may be off a bit, but you'll get the idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. additional arguments
    By l2u in forum C Programming
    Replies: 4
    Last Post: 06-08-2006, 08:24 AM
  2. Replies: 5
    Last Post: 03-21-2004, 03:18 PM
  3. completed a program; desire input
    By spirited in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2003, 08:50 AM
  4. Replies: 5
    Last Post: 08-22-2002, 03:32 PM
  5. Space Combat (50% Completed Program)
    By moonwalker in forum Game Programming
    Replies: 2
    Last Post: 07-26-2002, 08:59 AM