1 Replies - 437 Views - Last Post: 28 November 2012 - 08:38 PM Rate Topic: -----

#1 raybrunell66  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-November 12

Display a grade based on points...

Posted 28 November 2012 - 07:35 PM

I am having a problem and looking for help. I have to list a letter grade based on points entered in a program. Everything works fine until I get to the area of 450 points or above, it won't display a grade. Here is my code.
Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub txtPoints_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPoints.Enter
        txtPoints.SelectAll()
    End Sub

    Private Sub txtPoints_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPoints.KeyPress
        ' accepts only numbers and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtPoints_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPoints.TextChanged
        lblGrade.Text = String.Empty
    End Sub

    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim intGrade As Integer
        Dim searchForPoints As Integer
        Dim intPoints() As Integer = {450, 400, 350, 300, 0}
        Dim strGrades() As String = {"A", "B", "C", "D", "F"}

        searchForPoints = Integer.Parse(Me.txtPoints.Text)


       
        If searchForPoints >= 0 Or searchForPoints <= 500 Then

            For intGrade = 0 To 4

                If searchForPoints < intPoints(intGrade) Then

                    lblGrade.Text = strGrades(intGrade + 1)
                



                End If

            Next intGrade
        End If

    End Sub


*** Edit ***

Please use code tags when posting code
:code:

This post has been edited by GunnerInc: 28 November 2012 - 07:40 PM
Reason for edit:: Added elusive code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Display a grade based on points...

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Display a grade based on points...

Posted 28 November 2012 - 08:38 PM

When programming, you must pay strict attention to the details of how you want to accomplish a task. Each step must do something that aids in the solution, and if any one step is wrong, the result may be unsatisfctory. Here are some comments on your code.

' In this line, you have no "E". Perhaps that is your intention, but I doubt it.

 Dim strGrades() As String = {"A", "B", "C", "D", "F"}

' This line is always true. Consider: 7000 is greater than 0
' and -345 is less than 500. The condition is satisfied no
'  matter what number is being tested

        If searchForPoints >= 0 Or searchForPoints <= 500 Then
            For intGrade = 0 To 4

' This line checks for searchforpoints being less than 450 in the first iteration.
' if searchforpoints has a value of 450. no grade will be output, because searchforpoints
' is greater than any of the grade levels.

                If searchForPoints < intPoints(intGrade) Then

'This line will not do the job. Consider:
' searchforpoints = 200. On the first iteration, it will set the Label to "B"
' because searchforpoints is less than 450. on the second iteration, label will be set to "C"
' and so on, setting the Label for "D" and "F" (no "E", you will recall).
' The label will remain set to "F" because 200 is not less than 0.
'You also need to look at the index of what you want to print.

                    lblGrade.Text = strGrades(intGrade + 1)
                End If

            Next intGrade


So, it's time to rethink your logic. You need to fix the Grades array, the Points array, and the two If statements. Give it a try and get back to us.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1