Home > other >  I couldn’t enter fractional value by my keyboard and catching exception
I couldn’t enter fractional value by my keyboard and catching exception

Time:10-07

I wrote simple console program to calculate expression but I couldn’t enter fractional data to my values.I dont know where is mistake maybe because I am new in c# but I would be glad if someone could help me. Btw maybe the problem is in double.TryParse() method?

using System;

namespace ConsoleApp
{
class Program {
  public static void Main(string[] args) 
  {
    Console.OutputEncoding = System.Text.Encoding.UTF8;

    Console.InputEncoding = System.Text.Encoding.UTF8;
    
    System.Globalization.CultureInfo customCulture =
    (System.Globalization.CultureInfo)
    System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
    customCulture.NumberFormat.NumberDecimalSeparator = ".";
    System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
    
    Console.WriteLine("Calculate expression");
 Console.ReadLine();
    double x, y, z, s;
    do
    {
         Console.WriteLine("Enter fractional x = ");
         Console.ReadLine(); 
  if (double.TryParse(Console.ReadLine(), out x)) break;
         else
         {
            Console.WriteLine("Error try again!");
          Console.ReadLine();
   }
    }
    while (true);
    do
    {
         Console.WriteLine("Enter fractional y = ");
         Console.ReadLine(); 
  if (double.TryParse(Console.ReadLine(), out y)) break;
         else
         {
            Console.WriteLine("Error try again!");
          Console.ReadLine();
   }
    }
    while (true);
    do
    {
         Console.WriteLine("Enter fractional z = ");
         Console.ReadLine(); 
  if (double.TryParse(Console.ReadLine(), out z)) break;
         else
         {
            Console.WriteLine("Error try again!");
          Console.ReadLine();
   }
    }
    while (true);
    s = Math.Pow(2, -x) * Math.Sqrt(x   Math.Pow(Math.Abs(y), 1 / 4)) * Math.Pow(Math.Exp((x - 1) / Math.Sin(z)), 1 / 3);
    Console.WriteLine($"Result: s = {s:F3}");
   Console.ReadLine();  
  }
  }
}

CodePudding user response:

By fractional data, I assume you enter something like 3/4. If so, you cannot parse it directly. You need to separate out the numerator and denominator and calculate the value.

please refer this link Convert a text fraction to a decimal

  • Related