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.
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.
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.
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.
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 ...
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With