SoftDev30_15
SoftDev30_15

Reputation: 503

Convert Fahrenheit to Celcius in C# with methods

Hello guys I am a newbie and I am currently a first year in Computer Science. We have been given an exercise in which we have to convert from Fahrenheit to Celcius with methods (professor advised no static). We have started with C#. Here is my code.

namespace Week3_Exe3._1{
public class Formula
{
    public double F;
    public double C;

    public void calculate(double F, double C)
    {
        C = (5 / 9) * (F - 32);

    }

    public static void Main(string[] args)
    {

    Console.WriteLine("Please enter the Fahrenheit you wish to convert");
        F = double.Parse(Console.ReadLine());

        Formula form = new Formula();

        Console.WriteLine(F + "Fahrenheit correspond to " + form.calculate() + "Degrees celcius");
    }
}
}

I am currently working with Visual Studio Community 2015 and in form.calculate, it reds out the calculate with the error

CS7036 C# There is no argument given that corresponds to the required formal parameter '' of 'Formula.calculate(double, double)'

I searched for it but I still do not understand what is missing. I created an instance to use, but it's not working. Can anyone give me answer?

Upvotes: 5

Views: 7400

Answers (5)

Soner Gönül
Soner Gönül

Reputation: 98750

Your calculate method expect 2 parameters but you try to call it without parameters. In my humble opinion, it shouldn't take two parameters at all. Just one fahrenheit parameter is enough so it can calculate celsius value.

Also 5 / 9 performs an integer division —it always discards the fractional part— so it will always return 0.

A static method should be enough for your case;

static double Celsius(double f)
{
    return 5.0/9.0 * (f - 32);  
}

Upvotes: 7

Ciara
Ciara

Reputation: 394

The right answer is not going to be much use to you unless you understand what parameters and return values are and how to use them.

In your definition of the calculate method

public void calculate(double F, double C)

the method is expecting two input parameters, F and C. This means when you call the method from your main method, you need to pass in two values between the parentheses:

form.calculate(F, C)

As others have pointed out, you really only need one parameter, for the Farenheight. Which brings us to the next bit - how do you get a value back for C? This is what return values are for. That little word in between public and calculate defines the return type for your method. void means nothing is returned. In your case you'll be wanting to return a double. So putting this together the method definition should look like this

public double calculate(double F)

Finally you must actually return the value at the end of your method:

double C = (5.0 / 9.0) * (F - 32);
return C;

Upvotes: 1

Mykola
Mykola

Reputation: 3363

As you already assign the value of C you can remove C parameter from your function defenition code. Than your function must be like.

public void calculate(double F)
{
    C = (5.0 / 9.0) * (F - 32.0);  
}

Than in your code

public class Formula
{
    // public double F; remove because F is used as parameter
    public double C;

    public void calculate(double F)
    {
        C = (5.0 / 9.0) * (F - 32.0);    
    }

    public static void Main(string[] args)
    {

        Console.WriteLine("Please enter the Fahrenheit you wish to convert");
        var   F = double.Parse(Console.ReadLine()); // Creating new variable

        Formula form = new Formula();

        form.calculate(F); // Pass parameter to our function

        Console.WriteLine(F + "Fahrenheit correspond to " + form.C /* retrive the results of calculation */ + "Degrees celcius"); 
    }
}

Upvotes: 0

Denis Kralj
Denis Kralj

Reputation: 633

Your code has a few holes, my suggested rewrite is this:

public class Formula
{
    public double F;
    public double C;

    public void calculate()
    {
        C = (5.0 / 9.0) * (F - 32);
    }

    public static void Main(string[] args)
    {
        Console.WriteLine("Please enter the Fahrenheit you wish to convert");
        Formula form = new Formula();
        form.F = double.Parse(Console.ReadLine());
        form.calculate();
        Console.WriteLine(F + "Fahrenheit correspond to " + form.C + "Degrees celcius");
    }
}

keep in mind that this code only works for F => C, not the other way around.

Upvotes: 1

Alex
Alex

Reputation: 21766

There a few errors in your code. First of all, your function only requires one input parameter, the temperature in Fahrenheit. After you have resolved this, you will find temperature of 100 Fahrenheit will return a temperature of 0 Celcius and this is obviously not correct. You need to modify your equation to use at least one fractional part otherwise C# will implicitly cast the values to integers.

using System;

namespace Week3_Exe3._1
{
    public class Formula
    {
        public double calculate(double F)
        {
            return (5.0 / 9.0) * (F - 32.0);
        }

        public static void Main(string[] args)
        {
            Console.WriteLine("Please enter the Fahrenheit you wish to convert");
            var F = double.Parse(Console.ReadLine());
            Formula form = new Formula();
            Console.WriteLine(F + " Fahrenheit correspond to " + form.calculate(F) + " Degrees celcius");
        }
    }
}

Upvotes: 1

Related Questions