Thread: Something is wrong with my box program :C

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

    Something is wrong with my box program :C

    Hello everyone!

    So my teacher asked us to make a box in a particular size based on the users input.

    Specifically he only wanted the top left and bottom of a box.

    But I happen to be really bad with while loops and stuff, but here is my program so far.

    Code:
    /*This programe creates a box based on input from user*/
    
    #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;
    
    while (newtop != num) {
    newtop = top + 1 ;
    printf("*");
    
    }
    while (side!= top) {
    oside = side + 1;
    printf("*\n");
    
    }
    while(bot!= top) {
    obot = bot + 1;
    printf("*");
    
    }
    
    return 0;
    }
    When I excute the program I see nothing but stars, however it compiles with no error.

    Any help would be appreciated and thank you in advance !

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your program is using newtop before it's been given a value (thus the stars all over). Global variables are assigned to 0 or '\0', but local variables are not, so you need to assign them to something, before you use them as a comparison in your program.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Thank you for responding!

    Here is my program with newside, otop, and obot being assigned something before the while statements.
    Code:
    /*This programe creates a box based on input from user*/
    
    #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 = top + 1 ;
    printf("*");
    
    }
    while (side!= top) {
    oside = side + 1;
    printf("*\n");
    
    }
    while(bot!= top) {
    obot = bot + 1;
    printf("*");
    
    }
    
    return 0;
    }
    However I still receive the same error of endless astricks :C

    Any more insight you can spare?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to increment (or decrement) some variable that you are testing, as it loops

    Code:
    while (newtop != num) {
    newtop = top + 1 ;
    printf("*");
     
    }
    The test is newtop equaling num. Top + 1 is a constant number, since top isn't being incremented (and shouldn't be imo).

    Just use newtop++; and you're OK. Keep it simple, always.

    Your second while loop has the same type of problem. Fix it also.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Thank you, I tried the ++ before but my noobish self did it wrong. Anyways, it only seems to be executing the first while statement
    ex.) linux3[6]% ./a.out
    Enter the size of a box: 10
    **********
    thank you by the way for responding!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did you fix the second while loop problem I mentioned yet?

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Yes i got it to work. thank you very much for your assistance!
    Last edited by Rukris; 10-25-2011 at 08:01 PM. Reason: realized my error

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    I hate to bother once again :C

    But if I wanted to a diagnol how would I be able too O:?

    Thanks if you do answer this you've already helped so much

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Inside the box, or outside it?

    Use code tags and show me a bit of what you want.

    And no, you don't hate to bother me, that badly! < rofl! >

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Currently my program prints something like this

    * * *
    *
    * * *

    With a diagnol it would look something like this
    * * * *
    * *
    * * [err for some reason it wont let me space this one out. Activate imagine! haha]
    * * * *

    Haha your right I don't hate it you probably do =P
    Last edited by Rukris; 10-25-2011 at 08:31 PM.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You didn't use code tags around your diagram, so I can't tell much from it. The forum software ruins diagrams if you don't use code tags.

    So edit your post, and click on the "Go Advanced" button in the bottom right hand corner, and highlight your diagram. Now click on the # icon just right of top dead center, in the header icon section of the advanced editor page.

    Then I can see WTF you're diagram is all about.

    I believe this is what you want to do, but not really sure:
    Code:
    #include <stdio.h>
    
    #define ROW 20
    #define COL 20
    
    int main(void){
       int r,c,diag=ROW-1;
    
       for(r=0;r<ROW;r++,diag--) {
          for(c=0;c<COL;c++) {
             if(r==0 || r==ROW-1) {
                putchar('*');
             }
             else if(c==0 || c==COL-1 || c==diag)
                putchar('*');
             else
                putchar(' ');     
          }
          putchar('\n');
       }
    
       putchar('\n');
       return 0;
    }
    Last edited by Adak; 10-25-2011 at 10:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what's wrong with this program?
    By fares jajeh in forum C Programming
    Replies: 5
    Last Post: 05-05-2011, 10:39 AM
  2. what is wrong with my program?
    By Farnaz in forum C++ Programming
    Replies: 29
    Last Post: 02-28-2011, 02:18 PM
  3. wrong with my FFT program
    By rpbear in forum C Programming
    Replies: 4
    Last Post: 04-05-2010, 11:27 PM
  4. What is wrong with this program?
    By cuba06 in forum C Programming
    Replies: 4
    Last Post: 11-24-2007, 02:45 AM
  5. What is wrong with this program ?
    By Nutshell in forum C Programming
    Replies: 8
    Last Post: 01-11-2002, 11:22 PM