0

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;
        }
    }

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I have ask user first their math operation selection then two number from the user
I have passed two parameters in DivideNumber(double number1, double number2)
you need to pass parameters to the called function your function is expecting 2 parameters(n1,n2) so provide it
I did made some changes but still have an error showing "returns void, a return keyword must not be followed by an object expression" for the return statement in the switch statement
the problem is that in switch case you are returning too which cause the ambiguity for compiler so do 1 thing create a variable of double like double result = add(n1,n2); this variable will hold the value and then print it on screen
1
  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

2 Comments

Yes I do need all the functions
If I change return AddNumber(); to return AddNumber(number1, number2) is showing the same error
0

This should do what your trying to accomplish.

4 Comments

I updated the code according to your suggestion. Switch statement is not working as it runs the default case. I cannot use int.TryParse as my prof. doesn't allow to use it. I am asked to create an application which run/execute until the user chooses to Quit (Loop till Sentinel value typed).
I am getting one more error ---Input String was not in correct format at line double n1 = Convert.ToDouble(Console.Readline());
That's what the double.TryParse() was for, to validate the user input. To build onto what I have given you, look into recursion and call a function each time until the user wants to exit the application. link
If anything, google is a programmers best friend... link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.