I need to write a program to calculate temperature

I need to write a program to calculate temperature from fahrenheit to

Page 1 of 1

4 Replies - 3266 Views - Last Post: 10 November 2009 - 11:41 AM Rate Topic: -----

#1 bgile  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 29-September 09

I need to write a program to calculate temperature

Post icon  Posted 09 November 2009 - 06:49 PM

I am writing a program to calculate Fahrenheit to Celsius and Celsius to Fahrenheit.
I just can not get my math to work. thanks for any help.
Option Explicit On
Option Strict On
Option Infer Off


Public Class MainForm
	Private Sub CalcFahren(ByRef ftemp As Integer)
		Dim totalTemp As Decimal
		totalTemp = CDec(CDbl(TempTextBox.Text) * 9 / 5 + 32)
	End Sub
	Private Sub CalcCelsisus(ByRef ctemp As Integer)
		Dim cTotalTemp As Decimal
		cTotalTemp = CDec((CDbl(TempTextBox.Text) - 32) * 5 / 9)

	End Sub
	Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
		Me.Close()
	End Sub
	Private Sub calcCelsButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles calcCelsButton.Click
		Dim temp As Decimal
		Call CalcCelsisus(CInt(temp))

		celsLabel.Text = temp.ToString("N2")

		TempTextBox.Focus()
		TempTextBox.SelectAll()

	End Sub

	Private Sub calcFehrenButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles calcFehrenButton.Click

		Dim secTemp As Decimal

		Call CalcFahren(CInt(secTemp))

		fahrenLabel.Text = secTemp.ToString("N2")

		TempTextBox.Focus()
		TempTextBox.SelectAll()

	End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: I need to write a program to calculate temperature

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5958
  • View blog
  • Posts: 23,227
  • Joined: 23-August 08

Re: I need to write a program to calculate temperature

Posted 10 November 2009 - 06:45 AM

Off to VB.NET.
Was This Post Helpful? 0
  • +
  • -

#3 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: I need to write a program to calculate temperature

Posted 10 November 2009 - 07:24 AM

You are not getting the correct data into the Sub and you are not retiurning any result from the calculation.
' A function is a Sub that returns a result - it can be returned with either a Return statement, or the name of the Function can be used as the variable that will automatically be returned.
	Private Function CalcFahren(ByRef ftemp As Decimal) As Decimal
		CalcFahren= CDec(ftemp) * 9 / 5 + 32
		' alternate - "Return CDec(ftemp) * 9 / 5 + 32"
	End Function

' and call it with
Dim resultTemp As Decimal
resultTemp  = CalcFahren(CDec(TempTextBox.Text))
' now output it somewhere
...


Was This Post Helpful? 0
  • +
  • -

#4 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Re: I need to write a program to calculate temperature

Posted 10 November 2009 - 07:32 AM

Starting with calcCelsButton.Click:
"temp" is not assigned a value before it is used, so an error will occur.
Suggest making CalcCelsisus a function and setting the value of temp to that function like:
temp = CalcCelsisus()


And the function
 ''''Watch out for the spelling, I have corrected it here
Private Function CalcCelsius() As Decimal
   Dim cTotalTemp As Decimal
   cTotalTemp = CDec((CDbl(TempTextBox.Text) - 32) * 5 / 9)
   Return cTotalTemp
End Function


Try that with the other button press and routine for calculating Fahrenheit.
Hope this helps



oops, sorry Mark, didn't see your reply

This post has been edited by dzone41: 10 November 2009 - 07:33 AM

Was This Post Helpful? 0
  • +
  • -

#5 Shadar  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: I need to write a program to calculate temperature

Posted 10 November 2009 - 11:41 AM

Your code is more complicated than it needs to be.
Take a look at the code below.

Public Class Form3

	Private Function CelciusToFahrenheit(ByVal Tc As Decimal) As Decimal
		Dim Tf As Decimal
		Tf = (9 / 5) * Tc + 32
		Return Tf
	End Function

	Private Function FahrenheitToCelcius(ByVal Tf As Decimal) As Decimal
		Dim tc As Decimal
		Tc = (5 / 9) * (Tf - 32)
		Return TC
	End Function

	Private Sub txtFahrenheit_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtFahrenheit.KeyUp
		If txtCelsius.Text <> String.Empty Then
			txtCelsius.Text = FahrenheitToCelcius(txtFahrenheit.Text)
		Else
			txtCelsius.Text = ""
		End If
	End Sub

	Private Sub txtCelsius_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCelsius.KeyUp
		If txtCelsius.Text <> String.Empty Then
			txtFahrenheit.Text = CelciusToFahrenheit(txtCelsius.Text)
		Else
			txtFahrenheit.Text = ""
		End If
	End Sub
End Class


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1