Search code examples
c#stack-overflow

Stack overflow with a loop c#


I am currently having problems with this loop. It becomes an infinite loop and i get a stack overflow error. This is for a interest rate trade swap application. i is the length of the trade and l is the increasing index.

private void button1_Click(object sender, EventArgs e)
        {
            int outp = 0;
            int i = int.Parse(tradeLength.Text);
            string month = "January";
            for (int l = 1; l <= i; l++)
            {
                Console.WriteLine("I iterated " + l + " Amount of times");
                if (l == 1)
                {
                    month = "January";
                }
                if (l == 2)
                {
                    month = "February";
                }
                if (l == 3)
                {
                    month = "March";
                }
                if (l == 4)
                {
                    month = "Aprll";
                }
                if (l == 5)
                {
                    month = "May";
                }
                if (l == 6)
                {
                    month = "June";
                }
                if (l == 7)
                {
                    month = "July";
                }
                if (l == 8)
                {
                    month = "August";
                }
                if (l == 9)
                {
                    month = "September";
                }
                if (l == 10)
                {
                    month = "October";
                }
                if (l == 11)
                {
                    month = "November";
                }
                if (l == 12)
                {
                    month = "December";
                }
                else
                {
                    month = "Null";
                    l = 1;
                }

Solution

  • The cause is the final else:

        if (l == 12) {
          month = "December";
        }
        else { // <- if l != 12 (e.g. l == 1) restart the loop 
          month = "Null";
          l = 1;
        }
    

    you want else if:

        if (l == 1)
        {
            month = "January";
        }
        else if (l == 2) 
        {
            ... 
        }
        ...
        else if (l == 12) 
        {
            ... 
        }
        else {
          month = "Null";
          l = 1;
        }
    

    Edit: Another problem (see FKEinternet's comment) is a user input: if i is greater than 12 l never reaches it. You have to either validate the user input:

        int i = int.Parse(tradeLength.Text);
    
        if (i > 12)
          i = 12;    // or ask for other value
    

    or use modular arithmetics:

        for (int index = 1; index <= i; index++) {
          int l = index % 12 + 1;
    
          if (l == 1)
          {
             month = "January";
          }
          else if (l == 2)
            ...
          else if (l == 12)
            ...
          else  
          {
            month = "Null";
            l = 1;
          }
        }