Please tell me why this is not working

New to FreeBASIC? Post your questions here.
Post Reply
Ansaberk
Posts: 1
Joined: Mar 21, 2017 11:19

Please tell me why this is not working

Post by Ansaberk »

i thought this code should work fine but its not. i don't know what to declare "choice" and "answer" as. integer dose not work. please hep

declare sub main
declare sub addd
declare sub subtract
main
sub main
dim choice
print "1.add"
print "2.subtract"
print "What is your choice." ;
if choice = 1 then
addd

elseif choice = 2 then
subtract

end if
end sub

sub addd
dim num1 as integer
dim num2 as integer
dim answer
input "what is the first number" ; num1
input "what is the second number" ; num2
answer = num1 + num2
end sub

sub subtract
dim num1 as integer
dim num2 as integer
dim answer as integer
input "what is the first number" ; num1
input "what is the second number" ; num2
answer = num1 - num2
end sub
sleep
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Please tell me why this is not working

Post by counting_pine »

Hi Ansaberk,
Integer should be fine, but I don't see an Input line where you read in a choice from the user. Could that be the problem?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Please tell me why this is not working

Post by MrSwiss »

@Ansaberk,

choice / answer --> are not FB key-words. Consult help (FB-documentation).
As counting_pine wrote, Input is, what you're looking for (most likely).

Could you please put the code into "code" - tags thanks. This is for better reading/copying etc.
Edit your post of before (use the "Code" - Button, above Text Input Field).

Sample code ... :

Code: Select all

' Add_Subtract.bas -- 2017-03-22, MrSwiss
Declare Sub main()
Declare Function _add(ByVal As Integer, ByVal As Integer) As Integer
Declare Function _sub(ByVal As Integer, ByVal As Integer) As Integer

main()

Sub main()
    Dim As String   choice
    Dim As Integer  num1, num2

    Print
    Input "what is the first  number: ", num1
    Input "what is the second number: ", num2
    Print
    Print "1.add"
    Print "2.subtract"
    Print
    Input "What is your choice? ", choice
    Select Case choice
        Case "1" : Print "result = "; _add(num1, num2)
        Case "2" : Print "result = "; _sub(num1, num2)
        Case Else
            Print "illegal choice! hit [Enter] to continue ... ";
            Sleep : Cls : main()
    End Select
    Print
    Input "run it again? [y/N]", choice
    If LCase(choice) = "y" Then Cls : main()
End Sub

Function _add(ByVal n1 As Integer, ByVal n2 As Integer) As Integer
    Return n1 + n2  ' or Function = n1 + n2
end Function

Function _sub(ByVal n1 As Integer, ByVal n2 As Integer) As Integer
    Function = n1 - n2  ' or _sub = n1 - n2
end Function
' ----- EOF -----
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Re: Please tell me why this is not working

Post by integer »

Ansaberk wrote:i thought this code should work fine but its not. i don't know what to declare "choice" and "answer" as. integer dose not work. please hep
...
sub main
dim choice
print "1.add"
print "2.subtract"
print "What is your choice." ;

if choice = 1 then
addd
elseif choice = 2 then
subtract
end if
...
see response from counting_pine.
My question would be: HOW do you get the value of choice into the program?
When you "dim choice" , FB sets the value to zero.
Is there a line missing: "INPUT CHOICE"
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Please tell me why this is not working

Post by MrSwiss »

Another example, a true Mini-Calculator, using Doubles instead of Integers:

Code: Select all

' MiniCalc.bas -- 2017-03-22, MrSwiss

' Declarations are not necessary if: the implementation is before
' the first call

Sub menu(ByVal clr As UByte)
    Print : Color clr
    Print Tab(4); "1 = add"
    Print Tab(4); "2 = subtract"
    Print Tab(4); "3 = multiply"
    Print Tab(4); "4 = divide"
    Print
    Print Tab(4); "q/Q to EXIT"
    Print : Color 7
End Sub

Sub main()
    Dim As String   choice, res = "   result = "
    Dim As Double   num1, num2

    Print
    Input " what is the first  number: ", num1
    Input " what is the second number: ", num2
    menu(11)  ' Sub call (11) one parameter: fore ground color 'bright-blue'
    Input " What is your choice? ", choice
    Print : Color 12    ' bright-red
    Select Case LCase(choice)
        Case "1" : Print res; Str(num1 + num2)  ' direct, instead of a 
        Case "2" : Print res; Str(num1 - num2)  ' Function call ...
        Case "3" : Print res; Str(num1 * num2)
        Case "4" : Print res; Str(num1 / num2)
        Case "q" : Exit Sub
        Case Else
            Print " illegal choice! hit [Enter] to continue ... ";
            Sleep : Cls : main()
    End Select
    Print : Color 7     ' grey (default)
    Input " run it again? [y/N] ", choice
    If LCase(choice) = "y" Then Cls : main()
End Sub

main()
' ----- EOF -----
Post Reply