x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1359

# Using C# Console Application to Convert Formulas

I am having problems writing the following program in C# Console Application.

How do I write a program that convert Celsius to Fahrenheit that will allow an user to input any temperature?
This is the formula:
Tf=()9/5*Tc)+32
Tc=temperature in Celsius
Tf=temperature in Fahrenheit

Write a program that convert Fahrenheit into Celsus with the following formula.
Tc=(5/9)*(Tf-32)
Tc=temperature in Celsius
Tf=temperature in Fahrenheit

Write a program to figure the area of a right triangle. Display the aea of the triangle.
The following formula:
Area=1/2ab

Write a program that will find the area of a circle.
Formula is the following:

Make a program that will find the volume of a sphere
The formula is the following:
Volume of a Sphere:

Write a program that will calculate the volume of a cone.  Using the following heights: 10, 20, 30 as check.
Using the following formula:

Write a program on how long it would take the Concord to travel around the earth.  Display how many hours it would take.
Hours traveled:
Ec=24,000 miles
Cs=1,100 mph
Hours=Ec/Cs
Ec=Earth Circumference
Cs=Concord Speed

0
Cocono1
• 14
• 10
• +1
1 Solution

Commented:
Homework huh?
0

Author Commented:
Yes, it has to be completed by tomorrow.  I just can't get the number right.
0

Commented:
The reason for the question about homework is that we can't do homework for you.  We can, however, point you in a particular direction and explain errors in code you've written.

That said, have you any code yet?
Can you display/prompt to the console?
Can you read from the console?
0

Commented:
If you're having trouble just getting started, take a look here:
http://lab.msdn.microsoft.com/express/vcsharp/develop/createconsoleapp/default.aspx
0

Author Commented:
This is what I do have, but I keep getting error messages when I try to build.

double Tc;
double Tf;
int FahrenheitTemp=0;
int CelsiusTemp=0;
int average = 0;T
string sFahrenheitTemp;

for (int i=0; i<5; i++)
{
Console.WriteLine("Please enter temperature in degrees Fahrenheit: ", FahrenheitTemp);
sFahrenheitTemp=Convert.ToString(9/5)*Tc+32);

}

CelsiusTemp = (5.0/9.0)*(average)-32);
Console.WriteLine("The equivalent temperture in Celsius is: ",CelsiusTemp);

return 0;
0

Commented:
A couple of things:

1) What's the T at the end of int average; for?

2) WriteLine is only good for displaying something

3) You do the calculation before reading in the temperature to be converted

4) You asked for degrees F, but use the formula to convert C to F

5) Console.ReadLine isn't part of your variable - its value is the read in string, so you'ld do:

6) Was the assignment extended to include averaging five temps?
0

Author Commented:
No the assignment was not extended to include averaging five temps. Tc stand for Temperature Celcius and Tf stands for Temperature fehranheit.
The biggist problem that I have is making the formula work within the program.
I rewrote is as:

double Tc, Tf;
string sTc, sTf;

Console.WriteLine("Please enter temperature in degrees Fahrenheit: ", Tf);
sTf=Convert.ToString(9/5)*Tc+32;

sTc=Convert.ToString(5/9)*Tf-32;

Console.WriteLine("The equivalent temperture in Celsius is: ", Tc);

0

Commented:
WriteLine() isn't like a Prompt(), in that you can specify the variable to plop the response into - all it does is display something, so you'll have to do an explicit ReadLine() to get a string of the temperature to convert:

Console.WriteLine("Please enter temperature in degrees Fahrenheit: ");  // Display message
sTf=ConsoleReadLine();                                                                      // Get string of degrees F

At this point, you have degrees F to convert, but in string form, so you need to convert to double Tf with:
Tf=System.Convert.ToDouble(sTf);

You then compute Tc and convert the result back to a string for displaying the result.

I'll save you a bit of trouble here.  Your variables are doubles.  This lets you accept and produce numbers like 98.6.  The expressions (9/5) and (5/9) use integers, and the result will be the integers 1 and 0 respectively.  Use (9.0/5.0) and (5.0/9.0) instead.
0

Author Commented:
I am getting an error message for the was the following is setup:
sTc=Convert.ToString(5.0/9.0)*Tf-32;

This is the error message.

C:\Documents and Settings\77tatec\Desktop\C# Projects\MathFormula\Class1.cs(23): Operator '*' cannot be applied to operands of type 'string' and 'double'

0

Commented:
Correct.
What that expression says is "convert 5.0/9.0 to a string, then multiply that string by the double Tf, then subtract 32.
See what this does:

sTc=Convert.ToString((5.0/9.0)*Tf-32);
0

Commented:

It needs to be:

sTC = ( ( (5.0/9.0) * Tf) - 32).ToString();

--OR--

sTC = Convert.ToString( ( (5.0/9.0) * Tf) - 32);

Conver.ToString(5.0/9.0) is a string, and Tf is a double.  You were trying to multiply the two, and you can't :-)

-Eric
0

Author Commented:

It's giving me the error message: Use of unassigned local variable 'Tf' for sTf=Convert.ToString( ( (5.0/9.0) * Tf) + 32);
I don't understand the error message because I have Tf assigned to a double.
0

Commented:
Sid you forget the:
Tf=System.Convert.ToDouble(sTf);
0

Commented:
And shouldn't that have been:
sTc=Convert.ToString( ( (5.0/9.0) * Tf) + 32
^^^
0

Commented:
Oops
sTc=Convert.ToString(  (5.0/9.0) * Tf + 32);
0

Author Commented:
No.  This is how I have the program set up.

double Tc, Tf;
string sTc, sTf;

sTf=Convert.ToString( ( (5.0/9.0) * Tf) + 32);
Tf=System.Convert.ToDouble(sTf);

Console.WriteLine("Please enter temperature in degrees Fahrenheit: ", sTf);

sTc = Convert.ToString( ( (5.0/9.0) * Tf) - 32);
Tc=System.Convert.ToDouble(sTc);

Console.WriteLine("The equivalent temperture in Celsius is: ", Tc);

0

Author Commented:
double Tc, Tf;
string sTc, sTf;

sTf=Convert.ToString( ( (5.0/9.0) * Tc) + 32);//That's the way it supposed to be

Console.WriteLine("Please enter temperature in degrees Fahrenheit: ", sTf);
Tf=System.Convert.ToDouble(sTf);

sTc = Convert.ToString( ( (5.0/9.0) * Tf) - 32);
Tc=System.Convert.ToDouble(sTc);

Console.WriteLine("The equivalent temperture in Celsius is: ", Tc);

0

Author Commented:
Be it's still does not recognize Tc
0

Commented:
* What's the purpose of the third line?  You haven't read in anything yet, so Tc is truly unvalued.  Scratch the whole line.
* The ,sTf near the end of the fourth line is not appropriate - scratch it.
* Line 8 isn't needed - you've already crated the output string.
* In line 9, concatenate the generated string thus:
Console.WriteLine("The equivalent temperture in Celsius is: "+sTc);

An alternative for:
sTc = Convert.ToString( ( (5.0/9.0) * Tf) - 32);
Tc=System.Convert.ToDouble(sTc);
Console.WriteLine("The equivalent temperture in Celsius is: ", Tc);

could be:
Tc=(5.0/9.0)*Tf)-32);
Console.WriteLine("The equivalent temperture in Celsius is: "+Tc.ToString());

Also, since you just displayed the result, there's no need to read another line:

0

Commented:
I know, I know, my parens are all floogledoogled:

Tc=(5.0/9.0)*Tf-32;

Damned bifocals.
0

Author Commented:
double Tf, Tc;
string sTc, sTf;

sTf=Convert.ToString( ( (5.0/9.0) * Tc) + 32);

Console.WriteLine("Please enter temperature in degrees Fahrenheit: ", sTf);

sTc=Convert.ToString( ( (5.0/9.0) * Tf) - 32);
Console.WriteLine("The equivalent temperture in Celsius is: "+Tc.ToString());

It looks better, but it's now it gives me the error message:
Use of unassigned local variable 'Tc'
Use of unassigned local variable 'Tf'
0

Commented:
double Tf, Tc;
string sTc, sTf;

sTf=Convert.ToString( ( (5.0/9.0) * Tc) + 32);     <------ you use Tc here before it has been given a value.
And what's the point of calculating anything before
the value to convert has been read in?  Scratch it.

Console.WriteLine("Please enter temperature in degrees Fahrenheit: ", sTf); <--- get rid of the ,sTf.  No matter how many
times you leave it there, it's not going to have
any effect.

sTc=Convert.ToString( ( (5.0/9.0) * Tf) - 32);
Console.WriteLine("The equivalent temperture in Celsius is: "+Tc.ToString()); <--- Since you've already created the output
string, use it instead of converting a variable
you haven't given a value to:
Console.WriteLine("The equivalent temperture in Celsius is: "+sTc);
0

Commented:
As ever, I left out something:

Tf=Convert.ToDouble(sTf); <------ add this here to give Tf a value.
sTc=Convert.ToString( ( (5.0/9.0) * Tf) - 32);
0

Author Commented:
double Tf, Tc;
string sTc, sTf;

Tf=Convert.ToDouble(sTf);//o.k. I gave it a value, but I still get the message.
sTf=Convert.ToString( ( (5.0/9.0) * Tc) + 32);
Console.WriteLine("Please enter temperature in degrees Fahrenheit: ");

sTc=Convert.ToString( ( (5.0/9.0) * Tf) - 32);

Tc=Convert.ToDouble(sTc);
sTc=Convert.ToString( ( (5.0/9.0) * Tf) - 32);
Console.WriteLine("The equivalent temperture in Celsius is: "+Tc.ToString());

I hope I did everything in order.
0

Commented:

1  double Tf, Tc;
2  string sTc, sTf;
3
4  Tf=Convert.ToDouble(sTf);//o.k. I gave it a value, but I still get the message.
5  sTf=Convert.ToString( ( (5.0/9.0) * Tc) + 32);
6  Console.WriteLine("Please enter temperature in degrees Fahrenheit: ");
8
9  sTc=Convert.ToString( ( (5.0/9.0) * Tf) - 32);
10
11  Tc=Convert.ToDouble(sTc);
12  sTc=Convert.ToString( ( (5.0/9.0) * Tf) - 32);
13  Console.WriteLine("The equivalent temperture in Celsius is: "+Tc.ToString());

Lines 4 and 5:
You're still doing calculations before you've read in any thing.  Why?  Get rid of them.

Line 6:
Good.

Line 9:
The only value you have so far is sTf - the input string of the Fahrenheit temperature to be converted.
Replace this line with a line to convert the string sTf to a double:
Tf=Convert.Todouble(sTf);

Line 11:
Here we can do the calculation.  Replace with:
Tc=(5.0/9.0) * Tf - 32;

Line 12:
No longer needed, get rid of it.  Besides, it was using Tf that was set in Line 4 to a value derived from string sTf that hadn't been read in yet.

Line 13:
Yes.

0

Commented:
Time for some reflection.

Before writing one line of code, you should have a good, written idea of the flow of the program. For this one you would have:

1) display prompt
3) convert to a type suitable for arithmetic calculations
4) do the calculations
5) display the result
6) quit
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.