Hi im trying to do this project for class on functions. I am little confused on what i am doing wrong.

Here is the assignment

1) Write a program to calculate the wind chill. The wind chill is calculated by using the ambient temperature and the wind speed.

Wind chill temp = 91.4 + (temp - 91.4)(.474 + .304 sqrt(Wind Speed) - .0203(wind speed))

2) write a program to convert a numerical grade to a letter grade.

below 70 -F
70-79 - C
80-89 - B
90-100 - A

Here is the form code. I got the first part working, but not the 2nd assignment.

Dim grade As Integer
        grade = TextBox1.Text
        Label3.Text = Lettergrade(grade)
    End Sub



Function Lettergrade(ByVal Numgrade As Integer) As Char
    If Numgrade < 70 Then
        Label3.Text = "F"
    ElseIf Numgrade < 79 Then
        Label3.Text = "C"
    ElseIf Numgrade < 89 Then
        Label3.Text = "B"
    ElseIf Numgrade < 100 Then
        Label3.Text = "A"
    End If
End Function




Public Class Form1

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim IntTemp, IntWS As Integer
        IntTemp = Val(TextBox1.Text)
        IntWS = Val(TextBox2.Text)
        Label1.Text = WindChill(IntTemp, IntWS)
    End Sub
    Function WindChill(ByVal Temp As Double, ByVal Wspeed As Double) As Double
        Return 91.4 + (Temp - 91.4) * (0.474 + 0.304 * Math.Sqrt(Wspeed) - 0.0203 * (Wspeed))
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim grade As Integer
        grade = TextBox1.Text
        Label3.Text = Lettergrade(grade)
    End Sub

    Function Lettergrade(ByVal Numgrade As Integer) As Char
        If Numgrade < 70 Then
            Label3.Text = "F"
        ElseIf Numgrade < 79 Then
            Label3.Text = "C"
        ElseIf Numgrade < 89 Then
            Label3.Text = "B"
        ElseIf Numgrade < 100 Then
            Label3.Text = "A"
        End If
    End Function

End Class

Thanks

Recommended Answers

All 3 Replies

The first comparison is fine but the other 3 are off a bit. The middle 2 should follow the pattern of the first using 80 and 90. The last one is outside that pattern and should use <=.

Function Lettergrade(ByVal Numgrade As Integer) As Char
    If Numgrade < 70 Then
        Label3.Text = "F"
    ElseIf Numgrade < 80 Then
        Label3.Text = "C"
    ElseIf Numgrade < 90 Then
        Label3.Text = "B"
    ElseIf Numgrade <= 100 Then
        Label3.Text = "A"
    End If
End Function

On a side note, even though VB can allow implicitly converting text to integers, it's a very bad habit to get into and can create no end of problems on large projects. The Integer class has the Parse and TryParse Functions to handle this sort of thing. I would suggest reading up on and learning these. Also learn about turning on Option Strict which will disallow this sort of thing.

I find that for this type of problem, a Select clause is clearer

Select Case NumGrade
    Case 0 - 69
        Label3.Text = "F"
    Case 70 - 79
        Label3.Text = "C"
    Case 80 - 89
        Label3.Text = "B"
    Case 90 - 100
        Label3.Text = "A"
End Select

@Pompy: Function always returns a value, which type it may be.

Label3.Text = Lettergrade(grade)

The construction of the function Lettergrade should be

Function Lettergrade(ByVal Numgrade As Integer) As Char
    Select Case NumGrade
        Case 0 - 69
            Return "F"
        Case 70 - 79
            Return "C"
        Case 80 - 89
            Return "B"
        Case 90 - 100
            Return "A"
        End Select
End Function

Hope it can help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.