r/visualbasic • u/Despresl144 • Oct 23 '18
I'm making a program to convert Celsius to Fahrenheit for school and I do not understand what is not working
'This is my code
'I think there is something wrong with the math because when I try and convert it, the numbers are not right.
'Also, I would like to make it that If the temperature is above 0° then the background image changes to a sun. And if it's under 0° then the background image changes to like snow or something.
'**********************************************************************
'Programmeur: Lucas Després *
'Date: Octobre 2018 *
'But: De convertir les unité de mesure de la température *
'a Celcius et Fahrenheit et vice versa *
'**********************************************************************
Public Class frmConversion
Private Sub frmConversion_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Crée un Integer pour Fahrenhei et Celcius
Dim intF As Integer
Dim intC As Integer
intC = Val(txtbxEntree1.Text)
intF = Val(txtbxEntree2.Text)
intF = (9 / 5) * (intC + 32)
intC = (5 / 9) * (intF - 32)
Me.txtbxEntree2.Text = intF
End Sub
End Class
'Please help me fix this
1
u/Despresl144 Oct 25 '18
I have changed my integer to a double and the conversion to C to F works but not the other way around
This is what my code looks like now
Public Class frmConversion
Private Sub frmConversion_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Crée un Integer pour Fahrenhei et Celcius
Dim intF As Double
Dim intC As Double
intC = Val(txtbxEntree1.Text)
intF = Val(txtbxEntree2.Text)
intF = (intC * 9.0 / 5.0) + 32
intC = (intF * 5.0 / 9.0) - 32
Me.txtbxEntree2.Text = intF
Me.txtbxEntree1.Text = intC
End Sub
End Class
1
u/Despresl144 Oct 25 '18
It seems to be the "Me.txtbxEntree1.Text = int" that screwes up the whole thing, but I can't convert F to C
5
u/Macrado Application Specialist Oct 23 '18
Your parentheses are off on intF.
intF = ((9 / 5) * intC) + 32
intC = (5 / 9) * (intF - 32)
1
u/chrwei Oct 23 '18
this is the trap is integer math. since your variables are integers, and your static values are integers, the result in each step of the math is also only an integer, no decimals.
usually you can add a ".0" to the static values and that trigger the result to be a Double and it'll do what you expect.. so, intF = (9.0 / 5.0) * (intC + 32)
2
u/Macrado Application Specialist Oct 23 '18
What you are referring to is an implicit conversion to integer values, that occurs during division, and only when the divisor is explicitly declared as an integer. Because the constant values are not actually defined, they have a variant type, and can remain decimal values after the division has occurred. This implicit conversion doesn't occur on multiplication, and so the math holds. The only thing wrong is that the parentheses are in the wrong places.
Otherwise, he could use doubles for added precision, if the program is expected to return them.
1
u/chrwei Oct 23 '18
Because the constant values are not actually defined, they have a variant type
is that new for vb.net? pretty sure they get implied to the least type in other versions, and pretty much all other languages.
3
u/Macrado Application Specialist Oct 23 '18
Most of my vb experience is with vb6, but there, anything not explicitly typed is treated as a variant.
2
u/andrewsmd87 Web Specialist Oct 23 '18
Change your variables from int to double. Ints don't have decimals so you lose that precision.
That was probably one point of this assignment, to learn that :)