Improve company productivity with a Business Account.Sign Up

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 806
• Last Modified:

# Visual Basic 6.0 knowledge of decision making and math operations

I'm taking  intro to programming in school using Visual Basic 6.0 I don't understand on how to do the problem below. If someone can assist me on how to solve the problem.

PROBLEM:
There are three common temperature scales: Fahrenheit, Celsius, and Kelvin. Converting from Fahrenheit to Celsius is done with this formula:

C = (F - 32) * 5 / 9
To convert from Celsius to Kelvin, you just add 273.16.
The form has one box, into which a floating-point value of degrees Fahrenheit will be entered. When the button is clicked, the program should print out the temperature in Fahrenheit, Celsius, and Kelvin. It should also report on the state (using one of the words solid, liquid, or gas) of Water, Aluminum, and Copper. The melting point and boiling point of these materials is as follows:

Material         Melting Point (C)       Boiling Point (C)
-----------------------------------------------------------
Aluminum               660.3                   2519
Copper                1085                     2562
Water                    0                      100
If "1500" is enter, then the picture box should print something like:

Fahrenheit: 1500.0
Celsius:  815.6
Kelvin: 1088.7
Aluminum: Liquid
Copper: Solid
Water: Gas

0
apr2505
Asked:
• 8
• 7
• 2
• +1
1 Solution

Microsoft SQL Server Developer, Architect, and AuthorCommented:
Just so you know, EE rules state that we can guide you in accomplishing your homework, but not actually do it for you.  EE is not a homework site.

So with that in mind, tell us how you think you should start.
0

Author Commented:
Ok I have this for a code so far

Private Sub cmdCompute_Click()
Dim celsius As Single
'Convert Celsius to Fahrenheit to Kelvin
picResults.Print "Celsius:"
picResults.Print "Fahrenheit:"
picResults.Print "Kelvin:"
End Sub
Private Function Fahrenheit(celsius As Single) As Single
'Convert Celsius to Fahrenheit
Fahrenheit = (9 / 5) * celsius + 32

Private Function Kelvin(celsius As Single) As Single
'Convert Celsius to Kelvin
celsius = (Fahrenheit - 32) * 5 / 9 + 273.16
End Function

I'm not sure  how to get the other information to work. in the picture box it just displays Celsius, Fahrenheit, and Kelvin.

0

Microsoft SQL Server Developer, Architect, and AuthorCommented:
Is it part of your assignment to write the results of these calculations into a picture box?  Under normal circumstances text boxes, or maybe labels, would be used.

>IF "1500" is entered
Likely entered into a text box, I'll call it txtCelsius for this example

Create a button, name it cmbCalculate (for lack of a more original name), create two textboxes txtFarenheit and txtKelvin, and have the button's click event do the following;

Private Sub cmdCalculate_Click()

'Convert the value in Celsius to Farenheit and Kelvin.

'Hey, looky here, code comments.  Create lots of these so you can understand your own code better.

txtFarenheit = Farenheit(Me.txtCelsius.Text)
txtKelvin = Kelvin(txtCelsius.Text)

End Sub
0

Author Commented:
For this problem its a picture box,  understand what you are saying however my professor gave us  a form  to enter the code in and that has one text box, one picture box, and one command button. So I'm unable to create anything else to the form.
0

Commented:
So, it's assumed that the text box always has celcius degrees in it?

if so, then it shouldn't be too hard.

you do the print like you've got but you need to append your formula results to each string

picResults.Print "Celsius:" + your_celsius_string

same thing with your Farenheit and Kelvin lines,

Double check your Kelvin calculation.  What you said above is correct about how to convert from C to K.  But your code doesn't reflect that.

0

Commented:
ooops, also check your requirements.  Your code looks like you're trying to convert from Celsius, however, your instructions say you're converting from Farenheit
0

Author Commented:
How would I  have the calculation reflected in the code? thats what I'm stuck on I dont know i'm lost.
0

Commented:
what I mean is you have this as a formula...

celsius = (Fahrenheit - 32) * 5 / 9 + 273.16

that's an accurate formula.

Youf need something like

Private Function Celsius(farenheit as Single) As Single
Celsius =  '  put your F tp C formula here
End Function

Private Function Kelvin(farenheit as Single) As Single
Kelvin=  '  put your F to K formula here
End Function

You've got the code, you've got the formulas,  you even have code executing formulas.
You just don't have them put together correctly.  Try again.  You're very close

0

Author Commented:
ok made changes and added some but my numbers arent popping up right
Here is what I have now. I'm not sure on how to get solid, liquid, and gas in the code. Everytime I hit the command button with the number 1500. 1500 is displayed for everything.

Private Sub cmdCompute_Click()
Dim Celsius As Single
Dim fahrenheit As Single
Dim Kelvin As Single
Dim Aluminum As Single
Dim Copper As Single
Dim water As Single
Dim solid, liquid, gas As String

Celsius = Val(txtDegrees.Text)
fahrenheit = Val(txtDegrees.Text)
Kelvin = Val(txtDegrees.Text)

picResults.Print "Celsius:"; Val(txtDegrees.Text)
picResults.Print "Fahrenheit:"; Val(txtDegrees.Text);
picResults.Print "Kelvin:"; Val(txtDegrees.Text)
picResults.Print "Aluminum:"; Val(txtDegrees.Text); Tab(20)
picResults.Print "Copper:"; Val(txtDegrees.Text); Tab(20)
picResults.Print "Water:"; Val(txtDegrees.Text); Tab(20)
End Sub

Private Function Celsius(fahrenheit As Single) As Single
Celsius = (9 / 5) * Celsius + 32
End Function

Private Function Kelvin(fahrenheit As Single) As Single
Kelvin = (fahrenheit - 32) * 5 / 9 + 273.16
End Function

0

Commented:
you're printing Val(txtDegrees.Text) for every line,  so of course you're printing whatever value you put into your text box.  You need to use your functions for the Celsius and Kelvin lines.

Review your Celsius formula,  it''s not referencing your parameter.

As for your aluminum, copper and water lines, you need to have functions for them as well.

I suggest one function for each substance.  it takes a a celsius parameter and returns the state.

since you already have a function to convert farenheit to celsius, simply call that function when you invoke the state functions.
0

Author Commented:
ok I'll try again I did have txtcelsius.Text etc but it didn't work  I'll keep tryin though thanks
0

Commented:
you don't want a separate txtbox for each output of the functions.

you have a farenheit text box so print the text from that on your farenheit line.
on your celsius line print the result of your celsius function
on your kelvin line print the result of your kelvin function
on your aluminum line print the result of your aluminum function
and so on
0

Commented:
You need to call your functions:

picResults.Print "Celsius:"; Celsius(Val(txtDegrees.Text))
picResults.Print "Fahrenheit:"; Val(txtDegrees.Text);
picResults.Print "Kelvin:"; Kelvin(Val(txtDegrees.Text))

picResults.Print "Aluminum:"; Val(txtDegrees.Text); Tab(20)
picResults.Print "Copper:"; Val(txtDegrees.Text); Tab(20)
picResults.Print "Water:"; Val(txtDegrees.Text); Tab(20)
0

Author Commented:
Ok I've those changes and when the program is ran it highlights Celsius and Kelvin stating a Compile Error; Expected Array. is there any missing or something made in error? Here is my Code  I'm also trying to match Aluminum, Copper, and water with Liquid, Solid, and Gas. As stated above. This is really making pull my hair out.

Private Sub cmdCompute_Click()
Dim Celsius As Single
Dim fahrenheit As Single
Dim Kelvin As Single
Dim Aluminum As Single
Dim Copper As Single
Dim water As Single
Dim solid, liquid, gas As Single

Celsius = Val(txtDegrees.Text)
fahrenheit = Val(txtDegrees.Text)
Kelvin = Val(txtDegrees.Text)

picResults.Print "Celsius:"; Celsius(Val(txtDegrees.Text))
picResults.Print "Fahrenheit:"; Val(txtDegrees.Text);
picResults.Print "Kelvin:"; Kelvin(Val(txtDegrees.Text))
picResults.Print "Aluminum:"; Celsius(Val(txtDegrees.Text); Tab(20))
picResults.Print "Copper:"; Celsius(Val(txtDegrees.Text); Tab(20))
picResults.Print "Water:"; Celsius(Val(txtDegrees.Text); Tab(20))
End Sub

Private Function Celsius(fahrenheit As Single) As Single
Celsius = (9 / 5) * Celsius + 32
End Function

Private Function Kelvin(fahrenheit As Single) As Single
Kelvin = (fahrenheit - 32) * 5 / 9 + 273.16
End Function
0

Commented:
I think you're trying to rush and guess at the answer.
look at your code, piece by piece.

Celsius = Val(txtDegrees.Text)
fahrenheit = Val(txtDegrees.Text)
Kelvin = Val(txtDegrees.Text)

you have 3 variables all set to the same value. That doesn't make sense.
plus, look through the rest of the code, even if those were correct, you never
use them!  Either don't declare and set the variables at all, or set them to the
values they need to be: celsius to celsius, farenheit to farenheit and kelving to kelvin.

picResults.Print "Celsius:"; Celsius(Val(txtDegrees.Text))
picResults.Print "Fahrenheit:"; Val(txtDegrees.Text);
picResults.Print "Kelvin:"; Kelvin(Val(txtDegrees.Text))

These are correct! If you decide to use the variables then change these lines
but otherwise these should work just fine.

picResults.Print "Aluminum:"; Celsius(Val(txtDegrees.Text); Tab(20))
picResults.Print "Copper:"; Celsius(Val(txtDegrees.Text); Tab(20))
picResults.Print "Water:"; Celsius(Val(txtDegrees.Text); Tab(20))

You're trying to get the words "solid", "liquid" or "gas"
but you're calling a function that returns a number.   So, that should be obvious to
you that these aren't correct.  However, it is a step in the right direction, because you're evaluation of the state of each is based on the celsius temperature you have above.

Private Function Celsius(fahrenheit As Single) As Single
Celsius = (9 / 5) * Celsius + 32
End Function

This doesn't make sense.  you don't use the farenheit parameter
and your self reference celsius to.  But not as a function.  This is where your syntax error is coming from

Private Function Kelvin(fahrenheit As Single) As Single
Kelvin = (fahrenheit - 32) * 5 / 9 + 273.16
End Function

This is correct!

Once you fix the above, you need something like the following for each substance
to determine the state.

Private Function AluminumState(Celsius As Single) As String
AluminumState  = ' figure this part out.
End Function

0

Author Commented:
o no I'm not rushing this I'm just trying to see where I'm going wrong I'm not used to VB its making me frustrated. I've made more changes this is what I have below. When I hit the command button to see if the calculations is correct. The answers a completely wrong. However when I do the calculations on a calculator its write. o boy

Private Sub cmdCompute_Click()

Celsius = (Fahrenheit - 32) * 5 / 9
Fahrenheit = (Celsius + 32) * 9 / 5
Kelvin = (Fahrenheit - 32) * (5 / 9) + 273.15

picResults.Print "Celsius:"; (Celsius)
picResults.Print "Fahrenheit:"; (Fahrenheit); Tab(20)
picResults.Print "Kelvin:"; (Kelvin)

picResults.Print "Celsius:"; (Celsius)
picResults.Print "Fahrenheit:"; (Fahrenheit); Tab(20)
picResults.Print "Kelvin:"; (Kelvin)

End Sub

0

Author Commented:
I finally see I what needed to do. Thanks
0

Commented:
Celsius = (Fahrenheit - 32) * 5 / 9

You haven't set Farenheit yet, so that's why this doesn't work.

Fahrenheit = (Celsius + 32) * 9 / 5

You don't need to convert Farenheit, you have that from your text box.  Set this first
then setl Celsius and Kelvin

Kelvin = (Fahrenheit - 32) * (5 / 9) + 273.15

The reason it seems like you're rushing is these problems  have had nothing to do with Visual Basic.  It's just a matter of examining each step.  What are you trying to do?  Does this step do that?

Programming is an art, but it's a methodical art.  Visual Basic doesn't change that.  Good luck in the rest of your studies.  Thanks for the points.  I'm glad I could help.

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.

## Featured Post

• 8
• 7
• 2
• +1
Tackle projects and never again get stuck behind a technical roadblock.