Home > Enterprise >  How to program an error message when the user inputs a decimal fraction into a double variable with
How to program an error message when the user inputs a decimal fraction into a double variable with

Time:10-03

Dear Amateur and Veteran Developers,

I'm a beginner who recently started learning C# a bit more seriously (I've already tipped my toes into the world of C# in high school, but now, I'm more serious about learning it). I've made a simple command prompt application as a homework for my High-level Programming Languages lesson at my university:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HW_002_01
{
class Program
{
    static void Main(string[] args)
    {
        
        Console.Write("Please enter the length of the triangle's a side (cm)! ");
        double a;
        bool a_if = double.TryParse(Console.ReadLine(), out a);
        Console.Write("Please enter the length of the triangle's b side (cm)! ");
        double b;
        bool b_if = double.TryParse(Console.ReadLine(), out b);
        Console.Write("Please enter the length of the triangle's c side (cm)! ");
        double c;
        bool c_if = double.TryParse(Console.ReadLine(), out c);

        if (a_if == true && a > 0 && b > 0 && c > 0)
        {
            double res1 = a   b;
            if (res1 > c)
            {
                Console.WriteLine("Triangle equality is present.");
            }
            else
            {
                Console.WriteLine("Triangle equality is not present.");
            }
        }
        else if (b_if == true && a > 0 && b > 0 && c > 0)
        {
            double res2 = a   b;
            if (res2 > c)
            {
                Console.WriteLine("Triangle equality is present.");
            }
            else
            {
                Console.WriteLine("Triangle equality is not present.");
            }
        }
        else if (c_if == true && a > 0 && b > 0 && c > 0)
        {
            double ossz3 = a   b;
            if (res3 > c)
            {
                Console.WriteLine("Triangle equality is present.");
            }
            else
            {
                Console.WriteLine("Triangle equality is not present.");
            }
        }
        else
        {
            Console.WriteLine("You cannot make a tringle with these numbers.");
        }

        Console.ReadKey();
    }
  }
}

This little program checks triangle equality in a triangle with the numbers that the user types in. It runs fine, but I just don't know how to make an error message about the program not being able to understand decimal fractions with a dot instead of a comma. This would be a nice cosmetic touch-up to my program to excuse my beginner-level spaghetti code.

I hope someone replies very soon.

CodePudding user response:

double.TryParse already check the number format by using your default culture. So if the dot doesnt exist in your culture number format, and a user inputs it instead of comma, double.TryParse returns false.

CodePudding user response:

I've written a method that will help you make sure that there is a comma and it depends on the read and the operation you did to read and then convert to double. Previously your reading line is:

bool a_if = double.TryParse(Console.ReadLine(), out a);

And now:

bool a_if = double.TryParse(isWithComma(Console.ReadLine()), out a);

In fact, the method will check if the comma is within the number, and if that will show an error message and the program will stop and I have added a red color to give more expression in the error, and at the same time it returns a string if the number is correct and does not contain a comma, then the program will complete without problems

This is the entire code after modification, and there is a variable (ossz3) that has shown me an error and I have modified it, please check it:

    using System;

namespace HW_002_01
{
    class Program
    {
        //New by: https://github.com/MohammadYAmmar
        public static string isWithComma(string value)
        {
            if (value.Contains(","))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Error: cannot be used comma instead of a dot.");
                Console.WriteLine("Please try again using the point  (dot) for example 3.3");
                System.Environment.Exit(0);
                return value;//Won't happen
            }
            else
            {
                return value;
            }
        }
        static void Main(string[] args)
        {
            Console.Write("Please enter the length of the triangle's a side (cm)! ");
            double a;
            bool a_if = double.TryParse(isWithComma(Console.ReadLine()), out a);
            Console.Write("Please enter the length of the triangle's b side (cm)! ");
            double b;
            bool b_if = double.TryParse(isWithComma(Console.ReadLine()), out b);
            Console.Write("Please enter the length of the triangle's c side (cm)! ");
            double c;
            bool c_if = double.TryParse(isWithComma(Console.ReadLine()), out c);

            if (a_if == true && a > 0 && b > 0 && c > 0)
            {
                double res1 = a   b;
                if (res1 > c)
                {
                    Console.WriteLine("Triangle equality is present.");
                }
                else
                {
                    Console.WriteLine("Triangle equality is not present.");
                }
            }
            else if (b_if == true && a > 0 && b > 0 && c > 0)
            {
                double res2 = a   b;
                if (res2 > c)
                {
                    Console.WriteLine("Triangle equality is present.");
                }
                else
                {
                    Console.WriteLine("Triangle equality is not present.");
                }
            }
            else if (c_if == true && a > 0 && b > 0 && c > 0)
            {
                //Old
                //double ossz3 = a   b;
                //New: Please check that the spelling is correct so that the code is correct for this variable as you wanted it to
                double res3 = a   b;
                if (res3 > c)

                {
                    Console.WriteLine("Triangle equality is present.");
                }
                else
                {
                    Console.WriteLine("Triangle equality is not present.");
                }
            }
            else
            {
                Console.WriteLine("You cannot make a tringle with these numbers.");
            }

            Console.ReadKey();
        
    }

    }
}

Good luck and have a good journey in learning C# :)

  • Related