man22
man22

Reputation: 35

Don't understand how to pass a number through a method calculation and bring back the finished product

what i wana do is loop the two vales celsius and farenheit in the one for loop. I can get the farenheit part to work, but not the celcius one. I want the program to go into the method 'FarenheirToCelcius' to work out the celcius and then bring it back to the other method. Then in the loop i want it to increase by one and then go back into the method to work that out and so on. so basically to loop the value through the method every time. I don't understand how to do this thank u in advance!

the main program is

 static void Main(string[] args)
    {
        TemperatureConversion tempcon = new TemperatureConversion();
        tempcon.Conversion();
    }

class TemperatureConversion
{       
    double finalfern;
    double celcius;
    double ferntwoo;
    double finalcel;

    public void Conversion()
    {
        for (double celcius = 0; celcius >= 0 && celcius <= 100; celcius++)
        {
            double fern;
            double ferntwo;
            double finalfern;

            fern = celcius * 9;
            ferntwo = fern / 5;
            finalfern = ferntwo + 32;

            Console.Write(" Ferenheit" + finalfern++);
            Console.WriteLine();

            double finalcel = FarenheirToCelcius(ferntwoo, +1);

            Console.Write("Celcius" + finalcel++);
        }
    }
    public double FarenheirToCelcius(double celciustwo, double celtwo)
    {
        celciustwo = ferntwoo - 32;
        celtwo = celciustwo * 5;
        finalcel = celtwo / 9;
        return finalcel;
    }
}

Upvotes: 0

Views: 60

Answers (3)

Dmitry Avtonomov
Dmitry Avtonomov

Reputation: 9489

In the first line of your FarenheirToCelcius the variable ferntwoo is not defined.

Why do you even want to do the conversion back and forth when you can just print Celsius as is?

Is that what you want?

static void Main(string[] args)
{
    TemperatureConversion tempcon = new TemperatureConversion();
    tempcon.Conversion();
}

class TemperatureConversion
{       
    public void Conversion()
    {
        for (int celcius = 0; celcius <= 100; celcius++)
        {
            Console.Write(celcius + "C :")

            double f = CelciusToFarenheit(celcius);
            Console.Write(" Fahrenheit: " + f);

            double c = FarenheitToCelcius(f);
            Console.WriteLine(" Celcius: " + c);
        }
    }

    public double CelciusToFarenheit(double c)
    {
        return c * 9 / 5 + 32;
    }

    public double FarenheitToCelcius(double f)
    {
        return (f - 32) * 5 / 9;
    }
}

Upvotes: 0

Murtaza Tahir Ali
Murtaza Tahir Ali

Reputation: 609

For loop itself will take care of the increment. you do not need to do the increment manually. You can just you one variable to hold values and convert to and from instead of using many variables and confusing yourself.

public double FahrenheitToCelcius(double fahrenheit)
{
    double cel;
    cel = fahrenheit - 32;
    cel = cel * 5;
    cel = cel / 9;
    return cel;
}

Upvotes: 0

D Stanley
D Stanley

Reputation: 152566

Seems like you have too many variables, and based on their names it's not clear what they are for. And you are re-using class-level fields in one method but not the other. Take it in steps - write a method that converts from C to F (which is currently embedded in your loop) and fix the method that converts from F to C (you are not using the right variables)

As a starting hint, here's a better method for converting F to C:

public double FahrenheitToCelcius(double fahrenheit)
{
    double celcius;
    celcius = fahrenheit - 32;
    celcius = celcius * 5;
    celcius = celcius / 9;
    return celcius;
}

Note that there's one input parameter (it's not clear what the second parameter was for) and only one local variable (which is reused for each step).

Now write the C to F function, then the loop. I would argue that the loop belongs in your main method instead of the conversion class, but the first goal is to get code that works.

Upvotes: 1

Related Questions