Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

I know that there is already an accepted answer but still trying it in one of my own ways. This is an answer using basic Q Basic statements. As told we are going to use locate command to format the output and align the menu to the center of the screen.

Before starting, you must know that unlike other programming languages, Q Basic has only 25 rows and 80 columns(both numbered from 0). Syntax for locate command: LOCATE ROW, COLUMN : STATEMENT

This is how you print a string in the middle of the screen: first calculate the length of the string, half of the string must be printed on the right hand side and the rest on the left hand side of the middle column (The middle column is 40). Therefore the column number will be :40 - (length of string / 2). It is not a problem if the column number differs by a number or two.

Type 1:

If you want the menu to be centered and the words to be in the following order(all first letters aligned together)

           (A)dd Numbers
           (S)ubtract Numbers
           (M)ultiply Numbers
           (D)ivide Numbers

Here we will calculate the column number using the string (M)ultiply Numbers or (S)ubtract Numbers as they have the largest number of characters in them. Therefore the column number is 40 - (18 / 2) = 31.

Then the command using If..Elseif..Else is...

LOCATE 2, 31 : PRINT "(A)dd Numbers"
LOCATE 3, 31 : PRINT "(S)ubtract Numbers"
LOCATE 4, 31 : PRINT "(M)ultiply Numbers"
LOCATE 5, 31 : PRINT "(D)ivide Numbers"
LOCATE 6, 31 : INPUT "ENTER OPTION" ; OPT$
INPUT BLNK 'user must press enter
CLS
'You don't need to worry about whether the string is in upper case or lower
'case as we are going to compare the value only of the string converted
'into upper case using ucase$ function 
IF UCASE$(OPT$) = "A" THEN
'The statements required for addition
ELSEIF UCASE$(OPT$) = "S" THEN
'The statements required for subtraction
ELSEIF UCASE$(OPT$) = "M" THEN
'The statements required for multiplication
ELSEIF UCASE$(OPT$) = "D" THEN
'The statements required for division
ELSE PRINT "INVALID OPTION"
END IF
END

The same program using case

LOCATE 2, 31 : PRINT "(A)dd Numbers"
LOCATE 3, 31 : PRINT "(S)ubtract Numbers"
LOCATE 4, 31 : PRINT "(M)ultiply Numbers"
LOCATE 5, 31 : PRINT "(D)ivide Numbers"
LOCATE 6, 31 : INPUT "ENTER OPTION" ; OPT$
INPUT BLNK 'user must press enter
CLS
OP$ = UCASE$(OPT$)
SELECT OP$
CASE "A"
'The statements required for addition
CASE "B"
'The statements required for subtraction
CASE "M"
'The statements required for multiplication
CASE "D"
'The statements required for division
CASE ELSE
PRINT "INVALID OPTION"
END SELECT
END

Type 2: If you want the menu to be centered for all the options, then like this:-

               (A)dd Numbers
             (S)ubtract Numbers
             (M)ultiply Numbers
              (D)ivide Numbers

Then the code is...(but I still prefer the former one to be used, it looks a lot better) Using If..elseif...end

    LOCATE 2, 34 : PRINT "(A)dd Numbers"
    LOCATE 3, 31 : PRINT "(S)ubtract Numbers"
    LOCATE 4, 31 : PRINT "(M)ultiply Numbers"
    LOCATE 5, 32 : PRINT "(D)ivide Numbers"
    LOCATE 6, 33 : INPUT "ENTER OPTION"; OPT$
    INPUT BLNK 'user must press enter
    CLS
    IF UCASE$(OPT$) = "A" THEN
    'The statements required for addition
    ELSEIF UCASE$(OPT$) = "S" THEN
    'The statements required for subtraction
    ELSEIF UCASE$(OPT$) = "M" THEN
    'The statements required for multiplication
    ELSEIF UCASE$(OPT$) = "D" THEN
    'The statements required for division
    ELSE PRINT "INVALID OPTION"
    END IF
    END

The same program using Case...

    LOCATE 2, 34 : PRINT "(A)dd Numbers"
    LOCATE 3, 31 : PRINT "(S)ubtract Numbers"
    LOCATE 4, 31 : PRINT "(M)ultiply Numbers"
    LOCATE 5, 32 : PRINT "(D)ivide Numbers"
    LOCATE 6, 33 : INPUT "ENTER OPTION"; OPT$
    INPUT BLNK 'user must press enter
    CLS
    OP$ = UCASE$(OPT$)
    SELECT OP$
    CASE "A"
    'The statements required for addition
    CASE "B"
    'The statements required for subtraction
    CASE "M"
    'The statements required for multiplication
    CASE "D"
    'The statements required for division
    CASE ELSE
    PRINT "INVALID OPTION"
    END SELECT
    END

And make sure you fill the places will required statements instead of the ones like ' Statements required for addition. Hope this helps...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top