JustBasic, getting "bad nesting error select/case" in code

73 Views Asked by At

Pretty new to programming, sorry if basic question, first time doing modular work. Not sure what I'm doing wrong here, but line 41 ([ShowBalance] --> Case"C" spits out the error in the title when I try to run my program. What is nested improperly?

Balance=1000
Print "Banking Menu"
Do
Gosub[Menu]
Gosub[Deposit]
Gosub[Withdrawal]
Gosub[ShowBalance]
Loop Until Choice$="Q"
Print "Thanks for using the Banking Application"
End

[Menu]
Print "A. Deposit"
  Print "B. Withdrawal"
  Print "C. Show Balance"
  Print "Q. Quit"
  Input "Make Selection: ";Choice$
  Select Case Upper$(Choice$)
Return

[Deposit]
Case "A"
      Input "Enter Amount to Deposit: ";Dep
      If Dep > 0 Then
        Balance=Balance+Dep
      Else
        Print "Invalid Amount"
      End If
Return

[Withdrawal]
Case "B"
        Input "Enter Amount to Withdraw: ";Wdrw
        If Wdrw <= 1000 Then
            Balance=Balance-Wdrw
        Else
            Print "Invalid Amount"
Return

[ShowBalance]
Case "C"
      Print
      Print "*********************************"
      Print "*** Account Balance: $";using("####.##",Balance);" ***"
      Print "*********************************"
  End Select
  Print
  Print "----------------------------"
Return

1

There are 1 best solutions below

0
Sep Roland On

The Case instructions belong to the Select Case instruction. They must not get separated! Your program must always respect below layout without intervening Return instructions.

  Select Case <expression>
    Case <values>
      <action>
    Case <values>
      <action>
    Case <values>
      <action>
  End Select

Solution without subroutines

You insert the instructions that perform the action directly below the corresponding Case instruction. Disadvantage is that, if the number of instructions in an action block is elevated, you loose track of the whole Select Case construct.

Balance=1000
Print "Banking Menu"
Do
  Gosub[Menu]
Loop Until Choice$="Q"
Print "Thanks for using the Banking Application"
End

[Menu]
  Print "A. Deposit"
  Print "B. Withdrawal"
  Print "C. Show Balance"
  Print "Q. Quit"
  Input "Make Selection: ";Choice$
  Select Case Upper$(Choice$)
    Case "A"
      Input "Enter Amount to Deposit: ";Dep
      If Dep > 0 Then
        Balance=Balance+Dep
      Else
        Print "Invalid Amount"
      End If
    Case "B"
      Input "Enter Amount to Withdraw: ";Wdrw
      If Wdrw <= 1000 Then
        Balance=Balance-Wdrw
      Else
        Print "Invalid Amount"
      End If
    Case "C"
      Print
      Print "*********************************"
      Print "*** Account Balance: $";using("####.##",Balance);" ***"
      Print "*********************************"
      Print
      Print "----------------------------"
  End Select
Return

Solution with subroutines Deposit, Withdrawal, and ShowBalance

You place the instructions that perform the action in separate subroutines and insert a mere Gosub directly below the corresponding Case instruction. Advantage is that, even if the number of instructions in an action block is elevated, you keep a clear view on the whole Select Case construct.

Balance=1000
Print "Banking Menu"
Do
  Gosub[Menu]
Loop Until Choice$="Q"
Print "Thanks for using the Banking Application"
End

[Menu]
  Print "A. Deposit"
  Print "B. Withdrawal"
  Print "C. Show Balance"
  Print "Q. Quit"
  Input "Make Selection: ";Choice$
  Select Case Upper$(Choice$)
    Case "A"
      Gosub[Deposit]
    Case "B"
      Gosub[Withdrawal]
    Case "C"
      Gosub[ShowBalance]
  End Select
Return

[Deposit]
  Input "Enter Amount to Deposit: ";Dep
  If Dep > 0 Then
    Balance=Balance+Dep
  Else
    Print "Invalid Amount"
  End If
Return

[Withdrawal]
  Input "Enter Amount to Withdraw: ";Wdrw
  If Wdrw <= 1000 Then
    Balance=Balance-Wdrw
  Else
    Print "Invalid Amount"
  End If
Return

[ShowBalance]
  Print
  Print "*********************************"
  Print "*** Account Balance: $";using("####.##",Balance);" ***"
  Print "*********************************"
  Print
  Print "----------------------------"
Return