C# Pass an integer variable with an optional default value

Viewed 343
class Program
{
    static void Main(string[] args)
    {
        WriteLine("What is the radius of your circle: ");
        WriteLine("The area of your circle is: " + 
            circleArea(Double.Parse(ReadLine())).ToString());
        ReadKey();
    }

    static double circleArea(double radius = 5.00)
    {            
        return Math.PI * (radius * radius);
    }
}

I thought I had it set up correctly; however, I receive an error of System.FormatException: 'Input string was not in a correct format. on the line WriteLine("The area of your circle is: " + circleArea(Double.Parse(ReadLine())).ToString()); when no value is entered. I would like it to have a default value of 2. Thanks.

4 Answers

Your problem is that you need to split out the conversion to be able to test for a bad input condition. Take a look at this code.

            Console.WriteLine("What is the radius of your circle: ");
        var isNumber = Double.TryParse(Console.ReadLine(), out double number);
        if (!isNumber)
            number = 0;
        Console.WriteLine("The area of your circle is: " + circleArea(number).ToString());
        Console.ReadKey();

This will test for a legitimate number and if it's not, it just passes zero as the number.

Double.Parse() will always throw a FormatException if the input is not in the form of a valid double.

The behavior of default parameter values is that omitting the parameter when calling the method will cause it to instead use the default value (this is done by inserting the default value into the method call at compile-time). There is no language behavior which would enable an invalid value to be automatically replaced by some default.

In your case, you need to preempt the empty value which is going to Double.Parse(). Something like this:

class Program
{
    static void Main(string[] args)
    {
        WriteLine("What is the radius of your circle: ");

        var input = ReadLine();

        if (!double.TryParse(input, out var value))
            WriteLine($"Invalid input received: {value}");
        else
            WriteLine("The area of your circle is: " + circleArea(value).ToString());

        ReadKey();
    }
    static double circleArea(double radius = 5.00)
    {            
        return Math.PI * (radius * radius);
    }
}

I'd highly advice to do the reading and calculation in two steps

class Program
{
    static void Main(string[] args)
    {
        WriteLine("What is the radius of your circle: ");
        var input = ReadLine();
        double d = 0.0;

        if(!Double.TryParse(input,out d)) {
            d = //default value here
        }

        WriteLine("The area of your circle is: " + circleArea(d).ToString());
        ReadKey();
   }

}

Here's a concise way of testing the input and selecting a default if whatever was input is in an incorrect format.

        Console.Write("What is the radius of your circle: ");
        var value = double.TryParse(Console.ReadLine(), out var input) ? input : 2d;
        Console.WriteLine($"The area of your circle is {circleArea(value)}");
Related