Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Pass an integer variable with an optional default value

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.

like image 813
ciresuark Avatar asked Mar 09 '20 20:03

ciresuark


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


Video Answer


2 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.

like image 158
Frank Thomas Avatar answered Sep 30 '22 02:09

Frank Thomas


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);
    }
}
like image 44
Nathan Taylor Avatar answered Sep 30 '22 02:09

Nathan Taylor