Solved

Using C# Console Application to Convert Formulas

Posted on 2004-08-15
26
1,215 Views
Last Modified: 2012-06-27
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:
Area=Pi * radius squared

Make a program that will find the volume of a sphere
The formula is the following:
Baseball radius=1.5 inches
Earth radius=6278km
Volume of a Sphere:
Volume=(4 * Pi/3)* radius cubed

Write a program that will calculate the volume of a cone.  Using the following heights: 10, 20, 30 as check.
Using the following formula:
Volume=Pi*radius squared*height/3

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
Comment
Question by:Cocono1
  • 14
  • 10
  • +1
26 Comments
 
LVL 3

Expert Comment

by:pulupul
ID: 11804946
Homework huh?
0
 

Author Comment

by:Cocono1
ID: 11804964
Yes, it has to be completed by tomorrow.  I just can't get the number right.
0
 
LVL 22

Expert Comment

by:cookre
ID: 11805134
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
 
LVL 22

Expert Comment

by:cookre
ID: 11805158
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 Comment

by:Cocono1
ID: 11805205
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 adder = 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);
        sFahrenheitTemp.Console.ReadLine();
   
    adder = FahrenheitTemp + adder;
 
  }

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

Expert Comment

by:cookre
ID: 11805324
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:
sFahrenheitTemp=Console.ReadLine();

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

Author Comment

by:Cocono1
ID: 11805458
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
 
LVL 22

Expert Comment

by:cookre
ID: 11805550
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 Comment

by:Cocono1
ID: 11805709
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
 
LVL 22

Expert Comment

by:cookre
ID: 11805742
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
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11805761
You had "sTc=Convert.ToString(5.0/9.0)*Tf-32;" in your code.

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 Comment

by:Cocono1
ID: 11805787

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
 
LVL 22

Expert Comment

by:cookre
ID: 11805795
Sid you forget the:
Tf=System.Convert.ToDouble(sTf);
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 22

Expert Comment

by:cookre
ID: 11805803
And shouldn't that have been:
sTc=Convert.ToString( ( (5.0/9.0) * Tf) + 32
^^^
0
 
LVL 22

Expert Comment

by:cookre
ID: 11805810
Oops
sTc=Convert.ToString(  (5.0/9.0) * Tf + 32);
0
 

Author Comment

by:Cocono1
ID: 11805814
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);
sTf=Console.ReadLine();

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

Author Comment

by:Cocono1
ID: 11805842
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);
sTf=Console.ReadLine();
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);
                  
                  sTc=Console.ReadLine();
 
0
 

Author Comment

by:Cocono1
ID: 11805844
Be it's still does not recognize Tc
0
 
LVL 22

Expert Comment

by:cookre
ID: 11805885
* 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:
               sTc=Console.ReadLine();



0
 
LVL 22

Expert Comment

by:cookre
ID: 11805888
I know, I know, my parens are all floogledoogled:

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

Damned bifocals.
0
 

Author Comment

by:Cocono1
ID: 11805951
double Tf, Tc;
string sTc, sTf;
      
sTf=Convert.ToString( ( (5.0/9.0) * Tc) + 32);
            
Console.WriteLine("Please enter temperature in degrees Fahrenheit: ", sTf);
sTf=Console.ReadLine();
                            
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
 
LVL 22

Expert Comment

by:cookre
ID: 11805989
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.
sTf=Console.ReadLine();
                       
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
 
LVL 22

Expert Comment

by:cookre
ID: 11805993
As ever, I left out something:

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

Author Comment

by:Cocono1
ID: 11806060
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: ");
sTf=Console.ReadLine();
                       
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
 
LVL 22

Expert Comment

by:cookre
ID: 11806135
Your code as posted above:

  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: ");  
  7  sTf=Console.ReadLine();
  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
 
LVL 22

Accepted Solution

by:
cookre earned 500 total points
ID: 11806144
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
2) read a value
3) convert to a type suitable for arithmetic calculations
4) do the calculations
5) display the result
6) quit
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
linearIn  challenge 23 65
What is Python programming? 3 64
White board coding practice 3 61
PHP question(s) about order of output 9 41
A short article about a problem I had getting the GPS LocationListener working.
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now