-2

Im probably missing something but i cant find a way to solve this simple problem..

im suppost to write a short code asking for a positive integer and then print out a pyramid using FOR statement, f.ex

Height: 0
Height: 4
   *
  ***
 *****
*******

Here is the code itself

using System; 

namespace starpyramid
{ 
    class program 
    { 
        static void Main() 
        { 
            Veryfy:
            Console.Write("Height: "); 
            int i = int.Parse(Console.ReadLine());
            if(i>0)
            {
            goto main;
            }
            else
                {
            goto Verify;
            }   

        main:
            for (int h = 1; h< luku1;h++) // main loop for the lines
                {
                for (int s = 1; s < luku1; s++) //for spaces before the stars
                    {
                        if (s == h)
                        {
                        break;
                        }
                        Console.Write(" ");
                    }
                Console.Write("*\n");
                }
    }   
    } 
}

Maybe someone could helpo me solving this

jackJoe
  • 11,078
  • 8
  • 49
  • 64
user3468632
  • 43
  • 1
  • 5
  • 4
    What is your problem? – Jeroen Vannevel Mar 27 '14 at 13:08
  • 10
    Oh dear god, **don't use goto statements**. I thought these had been eradicated by now? – Liam Mar 27 '14 at 13:10
  • 1
    There is absolutely *no* reason to use `goto` here. – crashmstr Mar 27 '14 at 13:10
  • 3
    I spy with my little eye, two `goto`s... That's not going to get you a lot of positives. In any case, I believe your issue is that you're only ever writing a single asterisk, and you add too many spaces (you should only print half of `h`). – Luaan Mar 27 '14 at 13:10
  • 1
    You need to tell us what the problem is. Are you getting an error, or is the program doing something you didn't expect? – Chris Mar 27 '14 at 13:10
  • 2
    Your first label is misspelled, so the program won't compile. – Dustin Kingen Mar 27 '14 at 13:10
  • 3
    is `luku1` ever defined anywhere? – Smeegs Mar 27 '14 at 13:11
  • Programming homeworks. Feels as if it was just yesterday. :) – Crono Mar 27 '14 at 13:12
  • What `luku1` stands for and where do you define it? – Crono Mar 27 '14 at 13:12
  • [this'll tell you what to do](http://www.c-sharpcorner.com/Blogs/10217/creating-pyramid-in-C-Sharp.aspx) – Liam Mar 27 '14 at 13:15
  • 2
    Ignoring the `goto`s (just put the target code inside the `if` and `else`), inside your outer loop, you need to determine how many spaces and how many stars to print. That's just arithmetic, and not what you're asking about, so I assume you can do it. After you know how many of each, you need two `for` loops inside the big one: One to print the spaces, and one to print the stars. (You could do both in one loop, but there's no reason for it.) – John C Mar 27 '14 at 13:15
  • [Here's a freebie if you can translate it](http://jsfiddle.net/Ez9we/) – musefan Mar 27 '14 at 13:23
  • [why you shouldn't use goto's](http://www.dotnettoad.com/index.php?/archives/20-when-not-to-use-the-goto-keyword.html) & [when you may use them](http://stackoverflow.com/questions/6545720/does-anyone-still-use-goto-in-c-sharp-and-if-so-why) though I kinda feel the answer should be never ever ever (the raptors will get you....) – Liam Mar 27 '14 at 13:27
  • Here is an [example](https://gist.github.com/Romoku/9808698) for you. – Dustin Kingen Mar 27 '14 at 14:46

1 Answers1

0

its work perfect,

using System; 

namespace starpyramid
{ 
    class program 
    { 
        static void Main() 
        { 
            Console.Write("Height: "); 
            int i = int.Parse(Console.ReadLine());
            if(i>0)
            {
            goto main;
            }
            else
            {
                Main();
            }   

            main:
            Console.Write("\n");
            for (int h = 1; h<= i;h++) // main loop for the lines
            {
                for (int s = h; s <= i; s++) //for spaces before the stars
                {
                    Console.Write(" ");
                }
                for(int j=1; j<(h*2); j=j+2)
                {
                    Console.Write("*");
                }
            Console.Write("\n");
            }
    }   
    } 
}

but without goto program also work see,

using System; 

namespace starpyramid
{ 
    class program 
    { 
        static void Main() 
        { 
            Console.Write("Height: "); 
            int i = int.Parse(Console.ReadLine());
            Console.Write("\n");
            for (int h = 1; h<= i;h++) // main loop for the lines
            {
                for (int s = h; s <= i; s++) //for spaces before the stars
                {
                    Console.Write(" ");
                }
                for(int j=1; j<(h*2); j=j+2)
                {
                    Console.Write("*");
                }
            Console.Write("\n");
            }
    }   
    } 
}
i'm PosSible
  • 1,373
  • 2
  • 11
  • 30
  • Sorry for bad example of mine but Ajay Patel got it! It does a pyramide but the stars should grow like 1 3 5 7 etc without the spaces between. Like in the example i posted in my first post – user3468632 Mar 27 '14 at 14:02
  • 2
    If i was the one grading the work i would lower the score just based on the use of "goto". No matter if the program works or not :) – Sandman Mar 27 '14 at 14:03
  • @Sandman, I think I would just hand it back to the student and tell them to do it again without using goto. - haha – reirab Mar 27 '14 at 16:23
  • ya i devlop above code without goto statement bt he require goto in his code so i mention it. – i'm PosSible Mar 27 '14 at 17:55
  • i edit my code see this. – i'm PosSible Mar 27 '14 at 17:59