You need to set the result variable to a known value (see below) because when the method exits it tries to return result which does not have a value.

double result = 0;

16,016,501 members

See more:

C#

static void Main(string[] args) { Console.WriteLine("Enter a number: "); Double num1 = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter a 2nd number: "); Double num2 = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter an operator like +,-,*,/ : "); string op = Console.ReadLine(); calculate(num1, num2, op); Console.ReadLine(); Console.WriteLine("thank you"); Console.ReadLine(); } static double calculate(double num1, double num2, string op) { double result; if (op == "+") { result =(num1 + num2); } else if (op == "-") { result= num1 - num2; } else if (op == "*") { result = num1 * num2; } else if (op == "/") { result = num1 / num2; } else { Console.WriteLine("Invalid operation"); } return result; } } }

I am building a simple calculator using Method.The problem is in the last line " return result;". Any help will be appreciated guys.

You need to set the result variable to a known value (see below) because when the method exits it tries to return result which does not have a value.

double result = 0;

double result = 0;

Permalink

Share this answer

Comments

Sharyar Javaid
23-Jun-22 4:25am

Thanks Tony, but I already tried that. If I set double result = 0; . Yes It runs then but it still does not show the calculations . Only lets the user input 2 numbers and then exits. it doesnt even show 0 as result of calculation.

Tony Hill
23-Jun-22 4:31am

You did not mention that in the original question.

ReplaceQuote:Thanks Tony, but I already tried that. If I set double result = 0; . Yes It runs then but it still does not show the calculations . Only lets the user input 2 numbers and then exits. it doesnt even show 0 as result of calculation.

withQuote:calculate(num1, num2, op);

C#

double result = calculate(num1, num2, op); Console.WriteLine("Result is : {0}", result);

Permalink

Share this answer

Comments

Sharyar Javaid
23-Jun-22 5:00am

Its working now! Can you please explain what you did there? especially what this line Console.WriteLine("Result is : {0}", result); is doing which my code without it couldn't do?Also I already defined a variable double for result in the 2nd method, why did you define it again in the main method since this line takes input from the 2nd method. doesn't it? thanks so much!

CPallini
23-Jun-22 5:09am

Your method correctly computes and returns a value that your original code simply discarded.

double result = calculate(num1, num2, op);gets such a value and stores it in result.

Console.WriteLine("Result is : {0}", result);shows the result to the world.

You need to check that num2 is not zero before dividing.

This has nothing to do with your original question but will generate a divide by zero error if the user enters 0 for the 2nd number and "/" for the operation (try it!). Your code should be:

instead of

You should always check that the denominator is not zero before dividing unless your calculation somehow assures that it cannot possibly be zero under any circumstances.

This has nothing to do with your original question but will generate a divide by zero error if the user enters 0 for the 2nd number and "/" for the operation (try it!). Your code should be:

else if (op == "/") && (num2 != 0.0)

instead of

else if (op == "/")

You should always check that the denominator is not zero before dividing unless your calculation somehow assures that it cannot possibly be zero under any circumstances.

Permalink

Share this answer

Comments

Sharyar Javaid
24-Jun-22 2:15am

THANKS man. but its still working brother beforehand. It doesnt give error but says infinity when divide by 0. which is right i guess.

matblue25
24-Jun-22 11:57am

A mathematician would say no. Anything divided by zero is undefined. If you divide a number by progressively smaller and smaller numbers, the result approaches infinity but that doesn't mean that dividing by zero equals infinity.

Wikipedia - Divide by zero

EDIT:

Also look at this thread - I didn't realize it but this is a well-known behaviour in C# .NET

CodeProject Thread on Divide by Zero

Wikipedia - Divide by zero

EDIT:

Also look at this thread - I didn't realize it but this is a well-known behaviour in C# .NET

CodeProject Thread on Divide by Zero

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900