Results 1 to 4 of 4

Thread: inputbox validation

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    65

    inputbox validation

    im having trouble try to validate the data in the inputbox. I am trying to make sure only integers/decimals are entered, but the code is failing to reconize it in the loop, and the loop dosent promt the user to a valid number. What am i doing wrong or this a weakness of the inputbox?

    Code:
    'Developer- John Nelson
    'Date- March 5, 2012
    'Application Name- Semester Final Averages
    'Purpose-This application allows an instructor to enter up to 10 project scores from a course 
    'for a semester compute a students average score. The application displays the final average
    'for the semester with the two lowest grades removed from the average. The eight scores are
    'sorted in order and final averages are written to a file named grades.txt
    Public Class SemesterFinalAverages
        'Grade Array
        Private decgrade(9) As Decimal
        'now we create the textfile
        Private write As New IO.StreamWriter("C:\2\grades.txt")
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '
            lblstudent.Text = ""
            
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'This will start the inputbox loop to request the instructor to input 10 grades. The program will store each
            'grade into the "decgrade" array. After looping done it will send the total of all grades to Calculategrade()
            'The program will write all the grades and the final grade in the grades.txt file
            Dim loopisdone As Boolean = False
            Dim decstudentsgrade As Decimal
            'Call the loop function
            loopisdone = Gradeinput()
            'Calculate students average
            decstudentsgrade = Calculategrade()
            'tell the filesystem to write the students grades into the grades.txt file and also write the average grade
            For Each item As Decimal In decgrade
                write.WriteLine(item)
            Next
            write.WriteLine("The Students Final Average is " & decstudentsgrade.ToString)
            write.Close()
    
        End Sub
        Private Function Gradeinput()
            'This function is responsible for storing each input that instructor inputs into the decgrade array, and sorting
            'the array after input is done.
            Dim loopdone As Boolean = True
            Dim strinputmessage As String = "Please enter grade number "
            Dim intcurrentgradeinput As Integer = 1
            Dim strerror As String = "Please enter the grade that is bewteen 0 and 100"
            Dim strvaluehold As String
            Dim decvaluehold As Decimal
            Dim decgradeinput As Decimal
    
            strvaluehold = InputBox(strinputmessage & intcurrentgradeinput)
            For decgradeinput = 1 To (decgrade.Length - 1)
                If IsNumeric(strvaluehold) Then
                    decvaluehold = Convert.ToDecimal(decvaluehold)
                Else
                    MsgBox(strerror)
    
                End If
                strvaluehold = InputBox(strinputmessage & intcurrentgradeinput)
            Next
            Return loopdone
        End Function
        Private Function Calculategrade()
            'This will calculate the students grade, it will sort the array and the lowest grades wont be counted in the students
            'final grade.
            Dim decstudentscalculatedgrade As Decimal
            Dim dectotalofstudentsgrade As Decimal
            Dim decdividebyeight As Decimal = 8
            Dim strstudentsgrade As String = "The students final average is "
    
            'sort the grade array to determine the two lowest grades that were entered into the array
            Array.Sort(decgrade)
    
            'add the whatever is in the higher 8 elements to the decimal dectotalofgrades since we dont count the two lower grades
            For grade As Integer = 2 To (decgrade.Length - 1)
                dectotalofstudentsgrade += decgrade(grade)
            Next
            'divide dectotalofstudents grade by 8 and put that value into decstudentscalculatedgrade
            decstudentscalculatedgrade = dectotalofstudentsgrade / decdividebyeight
    
    
            lblstudent.Text = strstudentsgrade & decstudentscalculatedgrade.ToString
            Return decstudentscalculatedgrade
    
        End Function
    End Class

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: inputbox validation

    Using an InputBox is normally the problem, but since this is clearly an exercise, you probably don't have any alternative.

    What you should be doing is using the Decimal.TryParse method rather than the two steps of IsNumeric followed by Convert.ToDecimal:
    Code:
    If Decimal.TryParse(strValueHold, decValueHold) Then
     'It was a decimal, and is now in decvalueHold
    Else
     'It was not a decimal, so show your messagebox and loop.
    End If
    Of course, this could also be written to get rid of the strValueHold by doing this:
    Code:
    If Decimal.TryParse(InputBox(strinputmessage & intcurrentgradeinput),decValueHold)
     'The InputBox returned a string that was a decimal and is now in decValueHold
    Else
     'The InputBox returned a string that was not a decimal, so show your messagebox.
    End If
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    65

    Re: inputbox validation

    ok i see, but shouldnt the visual basic throw the inputbox again and again until the user enters the right decimal before going to the next array?
    Last edited by cytotoxictcell; Mar 6th, 2012 at 12:48 PM.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: inputbox validation

    Right, the code I showed is what should be done within the loop. I didn't really look at your code enough to see that you don't actually have a loop in there. What you need is a Do...Loop. Roughly, it would look like this:
    Code:
    Dim flag as Boolean = True
    Do
     If Decimal.TryParse(InputBox(strinputmessage & intcurrentgradeinput),decValueHold)
      'The InputBox returned a string that was a decimal and is now in decValueHold
       flag= False
    Else
     'The InputBox returned a string that was not a decimal, so show your messagebox.
    End If
    Loop While Flag
    
    Return
    That is close, but not a really good solution. After all, using that code, if you pressed Cancel in the InputBox, then nothing would really happen because the loop would just show the InputBox again. A more robust solution would be to use the first snippet I posted and check that the InputBox returned something other than "", and only perform the Decimal.TryParse if it did. If InputBox returned "", then you would have to return something to indicate failure, but since your code doesn't show a return type for the function, I'm not sure what you would want to use there.

    Anyways, that's what makes it an exercise. The code shown here is closer to the best solution, but not quite all of it.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width