Loop Problem--help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bitong
    New Member
    • Sep 2006
    • 40

    Loop Problem--help

    I want to make an output like a Christmas Tree

    but...my code shows only half of it...
    *
    **
    ***
    ****
    *****
    What's wrong with my loop?

    for(x=1;x<=5;++ x){
    for(y=0;y<x;++y ){
    cout<<"*";
    }
    cout<<"\n";
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you need to print spaces before the *, 5 on first line, 4 on second, etc so you get the triangle indented

    Comment

    • bitong
      New Member
      • Sep 2006
      • 40

      #3
      Originally posted by horace1
      you need to print spaces before the *, 5 on first line, 4 on second, etc so you get the triangle indented
      just a follow up question...does C++ have a gotoxy(col, row) like in Turbo C? if not what does C++ have in lieu of the col, row in turbo c..thanx for the reply

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        C++ doesn't have this, but if you look up the conio header file, you can use the goto(x, y) function normally.

        Comment

        • bitong
          New Member
          • Sep 2006
          • 40

          #5
          Originally posted by horace1
          you need to print spaces before the *, 5 on first line, 4 on second, etc so you get the triangle indented
          where am i going to put the spaces? before the second for loop or after the two loop? am i going to use "\t"?

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by bitong
            where am i going to put the spaces? before the second for loop or after the two loop? am i going to use "\t"?
            Code:
            for(x=1;x<=5;++x){
              <<<   print spaces here!
            for(y=0;y<x;++y){
            cout<<"*";
            }
            cout<<"\n";
            }
            to print the spaces you would use a for() loop which decrements (unlike the * which increments)

            Comment

            • bitong
              New Member
              • Sep 2006
              • 40

              #7
              Originally posted by horace1
              Code:
              for(x=1;x<=5;++x){
                <<<   print spaces here!
              for(y=0;y<x;++y){
              cout<<"*";
              }
              cout<<"\n";
              }
              to print the spaces you would use a for() loop which decrements (unlike the * which increments)
              ok..thanx a lot for the replies.. =)

              Comment

              Working...