Home > OS >  How to code division such that denominator should not be blank or character or zero
How to code division such that denominator should not be blank or character or zero

Time:12-16

using System;

//Division of two numbers
//Validate that only numbers are entered and not characters
//Test the program as
//Enter character, numeric, blank
//Only in case of numeric program is execute step by step but in case of character and blank it will ask for Input
//The above needs to be checked for both the input
//Denomenator cannot be zero

namespace DivideUsingTryParse
{

    class Program
    {
        public static void Main(string[] args) 
        {
            int a, b = 0;


            Console.Write("Enter the Numerator: ");
            while (!int.TryParse(Console.ReadLine(), out a)) //Validate that only number is entered
            {
                Console.Clear();
                Console.Write("Invalid input");
                Console.ReadLine();
                Console.Clear();
                Console.Write("Enter the Numerator: ");
            }
            
                    
            while (true)
            {
                try
                {

                    Console.Write("Enter the Denomenator: ");
                    b = Convert.ToInt32(Console.ReadLine());
                    if (b > 0 || b == 9 || null )
                    {
                    
                        break;
                    }
                }
                catch (DivideByZeroException e)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                    Console.Clear();
                }
                              
                Console.Clear();
            }

            Console.Clear();
            Console.WriteLine("Enter the Numerator: {0}", a);
            Console.WriteLine("Enter the Denomenator: {0}", b);
            Console.WriteLine("The Division of two numbers is: {0}", a / b);



        }

    }
}

if (b > 0 || b == 9 || !=null || != character) The above I have just mentioned, but I want to that apart from allowing only numeric, it should also see that no blanks or characters are allowed. How can be achieved ?

Since the code runs it does not go to catch and directly throws inbuilt exception.

Please advise!

CodePudding user response:

you need to use int.TryParse method. which returns false in case of the input is not convertible to int. so you need to check for its return value.

and if it returns true then check for the out parameter value whether is it !=0

CodePudding user response:

if (b > 0 || b == 9 || null ) is not a valid syntax ... and Convert.ToInt32 can never return null. It either returns an int or throws an exception (but not a DivideByZeroException you are catching)

Why not just use int.TryParse as you do with the numerator? And if parsing works, just check if b == 0. If yes, repeat reading the value ...

Console.Write("Enter the Denominator: ");
while (!int.TryParse(Console.ReadLine(), out b) || b == 0) 
{
  Console.Clear();
  Console.Write("Invalid input");
  Console.ReadLine();
  Console.Clear();
  Console.Write("Enter the Denominator: ");
}
  •  Tags:  
  • c#
  • Related