Search code examples
menuqbasic

Making a Menu in QBasic


I am making a simple calculator in QBasic, for that want to implement a Menu, the practice which I followed was:

PRINT "Select an Option"
PRINT "1. Addition"
PRINT "2. Subtraction"
PRINT "3. Multiplycation"
PRINT "4. Division"
PRINT "Option No.: "
INPUT opt
CLS
SELECT CASE opt
        CASE 1
                PRINT "You have selected Addition"
                PRINT "Enter a no.:"
                INPUT n1
                PRINT "Enter second no.:"
                INPUT n2
                PRINT "The Sum is "; n1 + n2
        CASE 2
                PRINT "You have selected Subtraction"
                PRINT "Enter a no.:"
                INPUT n1
                PRINT "Enter second no.:"
                INPUT n2
                PRINT "Difference between "; n1; " and "; n2; " is "; n1 - n2
        CASE 3
                PRINT "You have selected Multiplycation"
                PRINT "Enter a no."
                INPUT n1
                PRINT "Enter second no.:"
                INPUT n2
                PRINT "Product is "; n1 * n2
        CASE 4
                PRINT "You have Selected Division"
                PRINT "Enter a no.:"
                INPUT n1
                PRINT "Enter second no.:"
                INPUT n2
                PRINT "The Quotient is "; n1 / n2; " and the remainder is "; n1 MOD n2
        CASE ELSE
                PRINT "Invalid Option Number"
END SELECT

But this time I want a more sophisticated one, like:
(A)dd Numbers
(S)ubtract Numbers
(M)ultiply Numbers
(D)ivide Numbers
This all in a Box that is centered on the Screen. I am using LOCATE, but I am not getting the results I want, and yes I am trying this without Graphics. I'm also using INKEY$ to get the key entered by user. I tried INPUT but it echoed the character typed by user.

EDIT: @user2864740, it means that I was not able to get the perfect coordinates.


Solution

  • Here is something for you. It is fairly basic (no pun intended), but it is worth looking at. I will say it is untested, so it may not work as intended. It should give you an idea on how to do things.

    k$ = ""
    CLS
    
    ' Print the menu and get the operation to perform.
    WHILE k$ = ""
    
        LOCATE 8, 30
        PRINT "Select an option:"
        PRINT
        LOCATE , 30
        PRINT "(A)ddition"
        LOCATE , 30
        PRINT "(S)ubtraction"
        LOCATE , 30
        PRINT "(M)ultiplication"
        LOCATE , 30
        PRINT "(D)ivision"
        PRINT
        LOCATE , 30
        PRINT "Choice: ";
    
        ' Wait for a key press.
        WHILE k$ = ""
            k$ = INKEY$
        WEND
    
        SELECT CASE k$
        CASE "A", "S", "M", "D", "a", "s", "m", "d"
            ' Valid option entered.
        CASE ELSE
            k$ = ""
            LOCATE 17, 30
            PRINT "Invalid choice. Try again."
        END SELECT
    
    WEND 'WHILE k$ = ""
    
    ' Making the choice uppercase means testing only "A"
    '     works instead of testing for "A" and "a".
    ' The same is true with the other choices.
    k$ = UCASE$(k$)
    
    PRINT "You selected: ";
    SELECT CASE k$
    CASE "A": PRINT "Addition"
    CASE "S": PRINT "Subtraction"
    CASE "M": PRINT "Multiplication"
    CASE "D": PRINT "Division"
    END SELECT
    

    Also, be careful when using INKEY$. Press an arrow key for example. An arrow key is an example of an extended key, and there are others too. This is why the loop is designed the way it is. LEN(INKEY$) > 1 when an extended key is pressed, unlike a letter or a number where LEN(INKEY$) = 1.

    If you need reference material, the wiki at http://www.qb64.net/wiki/ should still be reliable. There is a link to an index on that page, or you can use the search box in the navigation area on the left to find what you need.