5

Level:Novice

I'm writing a console program in C# that finds the area of circles, triangles, and trapezoids. I want to make sure the user only enters numbers, by using double.TryParse.

Here's the code:

        Console.WriteLine("AreaSolution finds the area of different shapes.");
        Console.ReadLine();
 ln1:   Console.WriteLine("To find area of circle, press c. To find area of triangle press t.For trapezoid, enter tr. ");

        var x = Console.ReadLine();
        switch (x)
        {
            case "c":
                Console.WriteLine("Enter radius of circle, in centimeters");
                var radius = Console.ReadLine();
                double rad = Convert.ToDouble(radius);

                if (double.TryParse(rad) == false)
                { Console.WriteLine("Numbers only!"); };


                Console.WriteLine("AREA: " + 3.14 * rad * rad + " cm sq.");
                Console.ReadLine();
                break;

            case "t":
                Console.WriteLine("Enter base, in centimeters");
                var bas = Console.ReadLine();
                double tbase = Convert.ToDouble(bas);


                Console.WriteLine("Enter height, in centimeters");
                var tHi = Console.ReadLine();
                double tHei = Convert.ToDouble(tHi);
                Console.WriteLine("AREA: " + (tbase * tHei / 2) + " cm sq.");
                Console.ReadLine();
                break;

            case "tr":
                Console.WriteLine("Enter the length of the top base, in cm");
                var tpbas = Console.ReadLine();
                double bas1 = Convert.ToDouble(tpbas);

                Console.WriteLine("Enter length of bottom base, in cm");
                var btmbase = Console.ReadLine();
                double bas2 = Convert.ToDouble(btmbase);

                Console.WriteLine("Enter height, in cm");
                var trHe = Console.ReadLine();
                double trH = Convert.ToDouble(trHe);

                Console.WriteLine("AREA: " + (bas1 + bas2) * trH / 2 + " cm sq.");
                Console.ReadLine();
                break;
            default:
                Console.WriteLine("Please enter a valid character:  c for circle, t for triangle, or tr for trapeziod.");
                break;


        }
        Console.WriteLine("Another computation?(Y/N)");
        string newComp = Console.ReadLine();

        switch (newComp)
        {
            case "y":
                goto ln1;
                break;
            case "n":
                return;
                break;
        }

However, on the line of "double.TryParse(rad)", I get an error:

No overload for method 'TryParse' takes one argument.

How can I fix this error? Thanks in advance. Your help is greatly appreciated.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75

3 Answers3

5
            double rad = Convert.ToDouble(radius);

            if (double.TryParse(rad) == false)

should be

            double rad;
            if (!double.TryParse(radius, out rad))

Use ! (not) instead of == false just as better coding practice.

Also, TryParse needs an out value (It does the parsing, no need for Convert.ToDouble and ... your Convert.ToDouble will error if it's not parseable - bad!)

mcmonkey4eva
  • 1,359
  • 7
  • 19
  • My post was first Argsajgjgsakgshh... this triple-matching-answer was inevitable I suppose. – mcmonkey4eva Jun 29 '13 at 02:46
  • 1
    Why is using `!` better than `==false`? Seems subjective to me. – chue x Jun 29 '13 at 02:54
  • `just as better coding practice.` - it's not really better, it's just generally the preferred coding style. You're not required to do it, but people might view your code as unprofessional if you don't. – mcmonkey4eva Jun 29 '13 at 02:58
3

TryParse returns a boolean, and uses an out parameter to return the parsed value

double rad;
if (!double.TryParse(radius, out rad))
{ 
    Console.WriteLine("Numbers only!"); 
}
Kevin Nacios
  • 2,843
  • 19
  • 31
1
double result;

if (double.TryParse("123.45", out result))
{
    Console.WriteLine(result);
}

there are two TryParse() method overloads

if you need to convert the string representation of a number in a specified style and culture-specific format to its double-precision floating-point number equivalent then you should use overload function:

if (double.TryParse("2.3", NumberStyles.Number, CultureInfo.CurrentCulture, out result))
{
    Console.WriteLine(result);
}

if (double.TryParse("3.4", NumberStyles.Any, CultureInfo.GetCultureInfo("en-US"), out result))
{
    Console.WriteLine(result);
}

if (double.TryParse("5,6", NumberStyles.Any, CultureInfo.GetCultureInfo("fr-CA"), out result))
{
    Console.WriteLine(result);
}

if (double.TryParse("7.8", NumberStyles.Any, CultureInfo.InvariantCulture, out result))
{
    Console.WriteLine(result);
}
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179