1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Loop to print this:#########

Discussion in 'C++' started by anuvab1911, Jun 27, 2011.

  1. anuvab1911

    anuvab1911 New Member

    Joined:
    Jun 19, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    I am a beginner in C++ and am creating a program in which I want to create a frame with a border using the character "#".

    The code I have written is as follows:

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int xc; //xc is the x-coordinate of the cursor
    for(xc=0;xc<40;xc++)
    gotoxy(xc,20);
    printf("#");
    getch();
    }


    But the program executes only with a single # on the screen.
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    Because your printf statement is outside of the loop as you have no curly braces and so only the nest statement is what is considered as looping statement.
     
  3. anuvab1911

    anuvab1911 New Member

    Joined:
    Jun 19, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Thankyou shabbir.I actually wanted to print this:
    Code:
    **********
    *        *
    *        *
    *        *
    *        *
    *        *
    *        *
    *        *
    *        *
    **********
    
    I got the code for the upper and lower lines but when I try to change the y-coordinate for the same using the above code,it gives output as follows:

    **********









    *******<20 stars>*******

    Please tell me how to proceed and do I need to replace the cursor somewhere to get it correct?
     
  4. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    You don't need to be using gotoxy for this.
     
  5. anuvab1911

    anuvab1911 New Member

    Joined:
    Jun 19, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Then how do I do that?
    I want a loop statement to carry out my task.
     
  6. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    Try using the loop inside a loop to print * and spaces and that should do it.
     

Share This Page