Home > Mobile >  Infinite Loop C# Fix Please
Infinite Loop C# Fix Please

Time:04-18

        Console.WriteLine("Mortgage Loan Calculator");
        Console.WriteLine("------------------------------------");

        C1 c1 = new C1();
        while (true)
        {
            bool continueLoop = true;
            do
            {
                try
                {
                    Console.WriteLine("Enter loan amount: ");
                    loanAmount = Convert.ToDouble(Console.ReadLine());
                    checkLoanAmount(loanAmount);
                    continueLoop = false;
                }
                catch (FormatException formatException)
                {
                    Console.WriteLine("\n"   formatException.Message);
                    Console.WriteLine("Please enter a double value. \n");
                    continueLoop = true;
                }
                catch (MyRangeException negativeNumberException)
                {
                    Console.WriteLine("\n"   negativeNumberException.test);
                }
                catch (Exception exception)
                {
                    Console.WriteLine("\n"   exception.Message);
                    Console.WriteLine("Input string was not in a correct format");
                    continueLoop = true;
                }

            } while (continueLoop);

            do
            {
                try
                {
                    Console.WriteLine("Enter loan amount: ");
                    years = Convert.ToDouble(Console.ReadLine());
                    checkLoanYears(years);
                }
                catch (FormatException formatException)
                {
                    Console.WriteLine("\n"   formatException.Message);
                    Console.WriteLine("Please enter a double value. \n");
                }
                catch (MyRangeException negativeNumberException)
                {
                    Console.WriteLine("\n"   negativeNumberException.Message);
                }
                catch (Exception exception)
                {
                    Console.WriteLine("\n"   exception.Message);
                    Console.WriteLine("Input string was not in a correct format");
                }

            } while (continueLoop);

            do
            {
                try
                {
                    Console.WriteLine("Enter loan amount: ");
                    interest = Convert.ToDouble(Console.ReadLine());
                    checkLoanInterest(interest);
                }
                catch (FormatException formatException)
                {
                    Console.WriteLine("\n"   formatException.Message);
                    Console.WriteLine("Please enter a double value. \n");
                }
                catch (MyRangeException negativeNumberException)
                {
                    Console.WriteLine("\n"   negativeNumberException.Message);
                }
                catch (Exception exception)
                {
                    Console.WriteLine("\n"   exception.Message);
                    Console.WriteLine("Input string was not in a correct format");
                }

            } while (continueLoop);
        }

So I'm trying to create a Loan Program with exception handling. I have the code working to where it takes the exceptions when I input in the wrong format. The problem that I'm having though is that it keeps it in an infinite loop asking for the loan amount instead of going to the next question. If anyone could give me some advice of what I'm doing wrong that would be greatly appreciated!

CodePudding user response:

Here is a partial list of things I see with your code:

  1. Class C1 does nothing and it is not used

  2. Outer while loop has no exit condition while(true) { } and no break; statement inside.

  3. Repeating (copy/paste) code for similar functionality to receive user inputs. Major clue when you see this pull the code in a function, and use a loop if necessary to call the function. Here I think you use need to manualy call the function three times.

  4. Using try{} catch{} as part of regular code instead of only for something actually un-expected. This is the reason C# has the int.TryParse(), float.TryParse() and decimal.TryParse() method, in order to branch your code depending if the input is valid or not

    if(float.TryParse(input, out var value)) 
    {
        // use float `value`
    } else {
       // invalid input
    }
    

CodePudding user response:

Exceptions are for, well, exceptional events. Nothing in a user input program like this should be exceptional.

Your code for the loanAmount seems to be right though. It's setting continueLoop = false; and that should allow the loop to exit. You haven't shown us checkLoanAmount (which should be named CheckLoanAmount) so I can't tell you if that's causing your problem.

In any case, here's how I would do this kind of app:

Console.WriteLine("Mortgage Loan Calculator");
Console.WriteLine("------------------------------------");

decimal ReadDecimal(string message, Func<decimal, bool> validator, string validation)
{
    while (true)
    {
        Console.WriteLine(message);
        if (decimal.TryParse(Console.ReadLine(), out decimal result) && validator(result))
        {
            return result;
        }
        Console.WriteLine();
        Console.WriteLine(validation);
        Console.WriteLine();
    }
}

decimal loanAmount = ReadDecimal("Enter loan amount: ", d => d > 0m, "Amount must be greater than zero.");
decimal years = ReadDecimal("Enter number of years: ", d => d > 0m, "Amount must be greater than zero.");
decimal interest = ReadDecimal("Enter interest rate: ", d => d >= 0m && d <= 100, "Amount must be between zero and 100.");
  •  Tags:  
  • c#
  • Related