Home > database >  want my output to have a error message when the user tries to enter decimal and an error message if
want my output to have a error message when the user tries to enter decimal and an error message if

Time:10-08

using System;

namespace LabExer2_CMS
{
    class Program
    {
        double fnum;
        double snum;
        double answer;
        string str;
        static void Main(string[] args)
        {
            double fnum;
            double snum;
            double answer;
            string str;

            Console.WriteLine("CALCULATOR");
            Console.WriteLine(" ");

            Console.WriteLine("First Number: ");
            fnum = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Select an operator: ( , -, * )");
            str = Console.ReadLine();

            Console.WriteLine("Second Number: ");
            snum = Convert.ToInt32(Console.ReadLine());

            if (str == " ")
            {
                answer = fnum   snum;
                Console.WriteLine("The answer is: " answer);
            }
            if (str == "-")
            {
                answer = fnum - snum;
                Console.WriteLine("The answer is: " answer);
            }
            if (str == "*")
            {
                answer = fnum * snum;
                Console.WriteLine("The answer is: "   answer);
            }

            Console.Write("PRESS ENTER TO EXIT");
            Console.ReadKey();


            
        }
    }
}

CodePudding user response:

if it helps you



using System;

namespace LabExer2_CMS
{
    class Program
    {
        double fnum;
        double snum;
        double answer;
        string str;
        
        
        static double getDouble(string msg) {
            
            // original:  Convert.ToInt32(Console.ReadLine());
            
            string input = ""; 
            double res = 0;
            bool isOk = false;
            
            while (!isOk) {
                
              Console.WriteLine(msg);
              input = Console.ReadLine();
              isOk = double.TryParse(input, out res);
              
              if(!isOk) { Console.WriteLine("Invalid input."); }
              
            }
            
            return res;
            
        }
        
        static string getOperator(string msg1, string[] operators) {
            
            string res = "";
            
            string msg2 = String.Join(",", operators);
            string msg = msg1   "("   msg2   ") ";
            
            string input = "";
            bool isOk = false;
            
            while (!isOk) {
                
              Console.WriteLine(msg);
              input = Console.ReadLine();
              isOk = Array.IndexOf(operators, input) >= 0;
              
              if(!isOk) { Console.WriteLine("Invalid input."); }
              
            }

            return res;
             
        }
        
        static void Main(string[] args)
        {
            double fnum;
            double snum;
            double answer;
            string str;

            Console.WriteLine("CALCULATOR");
            Console.WriteLine(" ");

            fnum = getDouble("First Number: ");

            string[] operators = new string [] {" ", "-", "*"};
            str = getOperator("Select an operator: ", operators);

            snum = getDouble("Second Number: ");

            if (str == " ")
            {
                answer = fnum   snum;
                Console.WriteLine("The answer is: " answer);
            }
            if (str == "-")
            {
                answer = fnum - snum;
                Console.WriteLine("The answer is: " answer);
            }
            if (str == "*")
            {
                answer = fnum * snum;
                Console.WriteLine("The answer is: "   answer);
            }

            Console.Write("PRESS ENTER TO EXIT");
            Console.ReadKey();

        }
    }
}

CodePudding user response:

The first answer is better, but if it's hard to understand, you may try this one. You may wrap the lines with Console.ReadLine() in try {} catch {} operator, then you need to initialize fnum and snum when you declare them. And you should check the operator as well.

static void Main(string[] args)
        {
            double fnum = 0;
            double snum = 0;
            double answer;
            string str;

            Console.WriteLine("CALCULATOR");
            Console.WriteLine(" ");

            Console.WriteLine("First Number: ");

            try
            {
                fnum = Convert.ToInt32(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("Error!");
                return;
            }

            Console.WriteLine("Select an operator: ( , -, * )");
            str = Console.ReadLine();
            if (str != " " && str != "-" && str != "*")
            {
                Console.WriteLine("Wrong operator!");
                return;
            }

            Console.WriteLine("Second Number: ");

            try
            {
                snum = Convert.ToInt32(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("Error!");
                return;
            }

            if (str == " ")
            {
                answer = fnum   snum;
                Console.WriteLine("The answer is: "   answer);
            }
            if (str == "-")
            {
                answer = fnum - snum;
                Console.WriteLine("The answer is: "   answer);
            }
            if (str == "*")
            {
                answer = fnum * snum;
                Console.WriteLine("The answer is: "   answer);
            }

            Console.Write("PRESS ENTER TO EXIT");
            Console.ReadKey();
        }
  •  Tags:  
  • c#
  • Related