Overload Method Error with Switch statement

The application shall perform the selected menu math operation on the two entered numbers and display the answer.You must use methods for each math operation and a decision must be used to pick which method should be executed. The application shall display an error message when a correct menu number is not entered. This is a decision where 1-5 are valid selections and everything else is “Invalid”. The application shall not allow division by zero. Do not perform the divide operation if the second number variable is zero, but instead display an error message.

I have written the code but the program is showing the following error: 1. No overload for method AddNumber takes 0 arguments. this error is showing for all math operation methods inside the switch statements. 2. For DivideNumber method - not all code paths return a value

class Program
{
    static void Main(string[] args)
    {
         double  n1 = Convert.ToDouble(Console.ReadLine());
        //string n2 = "";
       double n2 = Convert.ToDouble(Console.ReadLine());

        Console.Clear();
        Console.WriteLine("Simple Calculator");
        Console.WriteLine("\t 1) ADD");
        Console.WriteLine("\t 2) SUBTRACT");
        Console.WriteLine("\t 3) MULTIPLY");
        Console.WriteLine("\t 4) DIVIDE");
        Console.WriteLine("\t 5) QUIT");
        Console.Write("Enter Selection: ");

        int menuSelection =0;
      //  double total;

          //  menuSelection = Console.ReadLine();
            switch (menuSelection)
            {
                case 1:
                   // total = Convert.ToInt32(AddNumbers("Results:"));
                   Console.WriteLine(AddNumbers(n1,n2));
                    break;
                case 2:
                    Console.WriteLine(SubtractNumber(n1,n2));
                    break;
                case 3:
                    Console.WriteLine(MultiplyNumber(n1,n2));
                    break;
                case 4:
                    Console.WriteLine(DivideNumber(n1,n2));
                    break;
                case 5:
                    break;           

                default:
                    Console.WriteLine("Invalid Selection !");
                    Console.ReadLine();
                    return;
            } 

        //Ask user to enter two numbers
        Console.WriteLine();
        Console.Write("Enter Number 1:", n1);
      //  number1 = Console.ReadLine();
        Console.Write("Enter Number 2:", n2);
      //  number2 = Console.ReadLine();

    }
    public static double AddNumbers(double number1, double number2)
    {
        return number1 + number2;
    }

    public static double SubtractNumber(double number1, double number2)
    {
        return number1 - number2;
    }

    public static double MultiplyNumber(double number1, double number2)
    {
        return number1 * number2;
    }

    public static double DivideNumber(double number1, double number2)
    {
        if (number1 == 0 && number2 == 0)
        {
            Console.WriteLine("Cannot Divide by Zero.    Try Again");
            return 0;
        }
        else
        {
            return number1 / number2;
        }
    }

Answers


1st mistake You are taking input after calling switch cases

2nd mistake your function have two parameters so pass them.

like DivideNumber(number1,number2) in your switch case

hope you understand

one thing i notice is you are taking input in string and your function takes double parameters so you need to convert string to double.

Give you idea use double to take input like double n1 = Convert.ToDouble(console.readline()); as input from console is string.


  1. you are calling your function return AddNumbers();

    but have declared it as public static double AddNumbers(double number1, double number2)

    AddNumbers expected 2 numbers, you have passed 0.

  2. You don't return anything from DivideNumbers, though you have declared it to return a double. public static double DivideNumber

  3. The program makes no sense as it is. You are trying to do the operation before they've even given you the numbers. You should try to Rubber Duck this one yourself


This should do what your trying to accomplish.


Need Your Help

delphi TreeView - create nodes at run-time

delphi treeview

Can anybody tell me how to do the following:

Using JavaScript to parse RSS feed - Without use of external service?

javascript ajax parsing rss mediawiki

I've basically been running around all day trying to accomplish one goal, getting the RSS Feeds from two separate sites to display using Javascript. I've pretty much found dozens of sites that acco...