Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Highest and lowest Value(Visual Basic)

  • 08-01-2014 2:39pm
    #1
    Registered Users, Registered Users 2 Posts: 5,063 ✭✭✭


    Create a program that allows the user to enter a number of values. After Values have been entered put up message box for(not using an array here user imputs)
    • Highest Value
    • Lowest Value

    Here's where I am so far

    lowest = Val (InputBox("Enter number"))
    highest = Val(inputBox("Enter number"))

    for i = 1 To 2

    Value = Val(InputBox("Enter number"))
    if Value < lowest Then
    lowest = value
    Else
    If Value > lowest Then
    highest = value

    End if
    End if
    Next i

    MsgBox lowest
    MsgBox highest

    End Sub

    I have tried making a number of changes to the code but keep running into the same problem. It reports the lowest values correctly but only reports the highest values correctly if it is the first enter value.
    Bonus would be having labels on textboxes.
    Ran individually I can find the highest or lowest of entered values
    I have tried a few different things with the same result or returning a blank box for highest.


Comments

  • Registered Users, Registered Users 2 Posts: 1,724 ✭✭✭rock22


    your second if statement should test value against highest.

    Have you tried using min and max functions ( if they are available in Basic)


  • Registered Users, Registered Users 2 Posts: 2,494 ✭✭✭kayos


    if Value > highest Then
    highest = Value


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Difficult to exactly understand what you're looking for, however it sounds like you want that the user enters a series of numbers manually, and at the end is shown the highest and lowest values entered.

    If so, there's a few ways of doing this, but with my rather rusty VB, one that comes to mind is:
    Dim tempStr As String
    Dim i, tempNo, highNo, lowNo As Integer
    
    highNo = 0;
    lowNo = 0;
    
    For i = 1 To 5
        tempStr = inputBox("Enter number")
        If IsNumeric(tempStr) Then
            tempNo = Val(tempStr)
            If tempNo > highNo Then highNo = tempNo
            If tempNo < lowNo Then lowNo = tempNo
        End If
    Next
    
    MsgBox "Highest: " & highNo & vbCrLf & "Lowest: " & lowNo
    
    Your error was as kayos has highlighted, you're only ever doing comparisons upon the current lowest value, never the highest.
    rock22 wrote: »
    Have you tried using min and max functions ( if they are available in Basic)
    Don't think they do, but even if they do, they'd likely take arrays as inputs and these would be more hassle than is worth it in VB.


  • Registered Users, Registered Users 2 Posts: 5,063 ✭✭✭Greenmachine


    Thanks every one for your speedy responses. I had a response but it didn't post for some reason. Just more more thing with the above I was expecting to enter 3 numbers to arrive with the highest and the lowest values entered. Can't work out why changing that line
    kayos recommend is causing it to ask for 4 values to be entered.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Looking at it quickly, it's probably impossible to tell because you don't seem to have cut and pasted your code correctly from the original code (with the two End If statements, one after the other, it would throw up a syntax error and not run as things stand), so I don't know if it's possible to tell where this other error is coming from at present.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,494 ✭✭✭kayos


    Thanks every one for your speedy responses. I had a response but it didn't post for some reason. Just more more thing with the above I was expecting to enter 3 numbers to arrive with the highest and the lowest values entered. Can't work out why changing that line
    kayos recommend is causing it to ask for 4 values to be entered.

    You have 3 requests for input. For the life of me I don't understand why you ask and assign the first two inputs to the highest and lowest implicitly typed variables (hint FTLOG put option explicit on all your classes and learn to be explicit). Any way remove the first two requests and use initialize highest and lowest to 0 as corint has posted.

    The last request for input is in a for loop controlled by 1 to 2 i.e. two iterations so the input will b e requested twice.

    So in total you request 4 inputs and display two messages.


Advertisement