0

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();  
  }
  }
}
gunr2171
  • 16,104
  • 25
  • 61
  • 88
Ceh9_N
  • 13
  • 3
  • 1
    Please don't tag with multiple language if it doesn't cover multiple languages. This is not a C question. In addition, you're _sure_ you're using C# 4.0? That's _really_ old. – gunr2171 Oct 06 '22 at 17:16
  • 1
    Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details which can be done with a [mre]. Please [edit] your question to add these details into it or we may not be able to help. – gunr2171 Oct 06 '22 at 17:17

1 Answers1

0

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

WisdomSeeker
  • 854
  • 6
  • 8