0

I have attempted to create a calculator with FreeBasic. What is the problem with line 9 in my code? Line 6 says that the dim choice is default and not allowed while line 9 tells me the variable isn't declared.

1 declare sub main
2 declare sub add
3 declare sub subtract
4 main
5 sub main
6 dim choice
7 print "1.Add"
8 print "2.subtract"
9 input "what is your choice" ; choice
ylun.ca
  • 2,504
  • 7
  • 26
  • 47
alstonan25
  • 23
  • 8

1 Answers1

0

Your source code is quite incomplete. For example, it misses the data types. In FreeBASIC you choose between several data types depending on what kind of data you want to store (Integer, Double, String, ...). Moreover, you did not define how your sub programs actually should work. You didn't give any code what your procedure "subtract" should do.

Here's a working example of your small calculator:

' These two functions take 2 Integers (..., -1, 0, 1, 2, ...) and 
' return 1 Integer as their result.
' Here we find the procedure declarations ("what inputs and outputs
' exist?"). The implementation ("how do these procedures actually
' work?") follows at the end of the program.
Declare Function Add (a As Integer, b As Integer) As Integer
Declare Function Subtract (a As Integer, b As Integer) As Integer

' == Begin of main program ==

Dim choice As Integer
Print "1. Add"
Print "2. Subtract"
Input "what is your choice? ", choice

' You could use "input" (see choice above) to get values from the user.
Dim As Integer x, y
x = 10
y = 6

If (choice = 1) Then
    Print Add(x, y)
ElseIf (choice = 2) Then
    Print Subtract(x, y)
Else
    Print "Invalid input!"
End If

Print "Press any key to quit."
GetKey
End

' == End of main program ==


' == Sub programs (procedures = subs and functions) from here on ==

Function Add (a As Integer, b As Integer) As Integer
    Return a + b
End Function

Function Subtract (a As Integer, b As Integer) As Integer
    Return a - b
End Function
MrSnrub
  • 1,123
  • 1
  • 11
  • 19