-8

I tried to make a multicalculator which could hold a lot of features like cos ton and conversions but I have been stuck with the following errors on the image for quite some time can someone please help? I am not sure what is causing the problem and I haven't learned how to use the debugging functionality yet.

The errors

The full code:

using System;

namespace Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the first number:");
            double num1 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter the second number (if applicable):");
            double num2 = Convert.ToDouble(console.ReadLine());

            Console.WriteLine("Enter the operation (A, S, -, +, M, /, C, T, H, L, LOG, IN, DEG, SYN, COT, CC, VC, LC, HC, DC, OC, BC):");
            string op = Console.ReadLine();

            Calculator calc = new Calculator(num1, num2);
            double result = calc.Calculate(op);
            Console.WriteLine("Result: " + result);
        }
    }

    class Calculator
    {
        private double num1;
        private double num2;

        public Calculator(double num1, double num2)
        {
            this.num1 = num1;
            this.num2 = num2;
        }

        public double Calculate(string op)
        {
            switch (op)
            {
                case "A":
                    return Math.Abs(num1);
                case "S":
                    return Math.Sqrt(num1);
                case "-":
                    return num1 - num2;
                case "+":
                    return num1 + num2;
                case "M":
                    return num1 * num2;
                case "/":
                    return num1 / num2;
                case "C":
                    return Math.Cos(num1);
                case "T":
                    return Math.Tan(num1);
                case "H":
                    return Hypotenuse(num1, num2);
                case "L":
                    return Math.Log(num1);
                case "LOG":
                    return Math.Log10(num1);
                case "IN":
                    return Math.Log(num1, num2);
                case "DEG":
                    return Math.ToDegrees(num1);
                case "SYN":
                    return Math.Sin(num1);
                case "COT":
                    return 1 / Math.Tan(num1);
                case "CC":
                    return CurrencyConverter(num1, num2);
                case "VC":
                    return VolumeConverter(num1, num2);
                case "LC":
                    return LengthConverter(num1, num2);
                case "HC":
                    return HexConverter(num1);
                case "DC":
                    return DecimalConverter(num1);
                case "OC":
                    return OctalConverter(num1);
                    break;
            }
        }
    }
}
E_net4
  • 27,810
  • 13
  • 101
  • 139
  • 1
    Well we need to see all the code. Also please paste the errors into the question. – pm100 Dec 25 '22 at 16:20
  • Relevant read: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ which is linked from [ask] – rene Dec 25 '22 at 16:22
  • seems like the issue is with the Calculate method , it's not returning a double value in all the cases , also you may be missing the default option of the switch operator , and finally , seems like the code you putting out there is not complete , please provide all the code of the calculate Method – Ferhi Malek Dec 25 '22 at 16:23
  • @FerhiMalek Would you like to provide me the corrected code? – John Adams Dec 25 '22 at 16:28
  • @JohnAdams I would if you provide the full code of the Calculate method in your question , without complete code , no one could help you – Ferhi Malek Dec 25 '22 at 16:32
  • @FerhiMalek Ok wait – John Adams Dec 25 '22 at 16:35
  • 3
    You have several compiler errors: each of them is solvable if you read the error message, and research in what situations that error can arise, then apply it to your situation. Is there a specific error that you can't figure out? Which one? What do you think it means? – mason Dec 25 '22 at 16:44
  • 1
    Does this answer your question? [C# compiler error: "not all code paths return a value"](https://stackoverflow.com/questions/21197410/c-sharp-compiler-error-not-all-code-paths-return-a-value) and [Tutorial: Learn to debug C# code using Visual Studio](https://learn.microsoft.com/en-us/visualstudio/get-started/csharp/tutorial-debugger?view=vs-2022) or [Tutorial: Debug a .NET console application using Visual Studio Code](https://learn.microsoft.com/en-us/dotnet/core/tutorials/debugging-with-visual-studio-code?pivots=dotnet-7-0) – Luuk Dec 25 '22 at 16:47

1 Answers1

-1

I will try to clarify some of the errors that are showing up in it :

  • When reading the second number , you are using "console" keyword which is not recognized , should be replaced by Console

  • The following functions that you are using in this code are not predefined in C# and are not defined in your code : Hypotenuse, ToDegrees, CurrencyConverter, VolumeConverter, LengthConverter, HexConverter, DecimalConverter, OctalConverter.

  • You need to include a default case at the end of the Calculate method code which returns a predefined value in case all of the other cases are not applicable : here is an example of the Calculate method that will work for you , ( as per the commented Cases such as ToDegrees, you will need to define those methods yourself, before calling them inside the Switch ) :

        public double Calculate(string op)
        {
            switch (op)
            {
                case "A":
                    return Math.Abs(num1);
                case "S":
                    return Math.Sqrt(num1);
                case "-":
                    return num1 - num2;
                case "+":
                    return num1 + num2;
                case "M":
                    return num1 * num2;
                case "/":
                    return num1 / num2;
                case "C":
                    return Math.Cos(num1);
                case "T":
                    return Math.Tan(num1);
                //case "H":
                //    return Hypotenuse(num1, num2);
                case "L":
                    return Math.Log(num1);
                case "LOG":
                    return Math.Log10(num1);
                case "IN":
                    return Math.Log(num1, num2);
                //case "DEG":
                //    return Math.ToDegrees(num1);
                case "SYN":
                    return Math.Sin(num1);
                case "COT":
                    return 1 / Math.Tan(num1);
                //case "CC":
                //    return CurrencyConverter(num1, num2);
                //case "VC":
                //    return VolumeConverter(num1, num2);
                //case "LC":
                //    return LengthConverter(num1, num2);
                //case "HC":
                //    return HexConverter(num1);
                //case "DC":
                //    return DecimalConverter(num1);
                //case "OC":
                //    return OctalConverter(num1);
                default:
                    return 0;
            }
        }
Ferhi Malek
  • 484
  • 4
  • 15