0
        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!

Daniel
  • 21
  • 2
  • Put a debug point and you can figure it out yourself. Part of programming is debugging too. – Ergis Apr 17 '22 at 22:43
  • Can you explain at least how to put a debug point? – Daniel Apr 17 '22 at 22:49
  • @MattVeler That varies depending on the IDE you are using. Some Googling will show you the way. Learning how to use your debugger is the best thing you can learn as a coder. – Lee Taylor Apr 17 '22 at 22:51
  • I actually figured out what was wrong. I feel stupid right now. I put the string as "enter loan amount for all of my loops, so the code was right. I just had to change the strings accordingly. – Daniel Apr 17 '22 at 22:53
  • 1
    You should be using the [`decimal` type](https://learn.microsoft.com/dotnet/api/system.decimal) — **not** `float` or `double` — to store currency. See [In .net, how do I choose between a Decimal and a Double](https://stackoverflow.com/q/2545567/150605) and [Why not use Double or Float to represent currency?](https://stackoverflow.com/q/3730019/150605). – Lance U. Matthews Apr 17 '22 at 22:56
  • Please try to make it without exception handling. You'll be a much better programming if you can do that. – Enigmativity Apr 17 '22 at 23:25
  • Code smell if your code handles exceptions in the regular course of actions expected. Use `bool ok = decimal.TryParse(input, out var value);` and then branch as needed. – John Alexiou Apr 17 '22 at 23:28
  • You have loops that depend on `continueLoop` to decide whether to continue looping or not, but the code body of the loop never sets `continueLoop=false` This will naturally lead to an infinite loop – Caius Jard Apr 18 '22 at 06:07

2 Answers2

0

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
    }
    
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
0

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.");
Enigmativity
  • 113,464
  • 11
  • 89
  • 172