Author Topic: Help - Illegal function call on a simple if  (Read 1361 times)

0 Members and 1 Guest are viewing this topic.

Offline 191Brian

  • Newbie
  • Posts: 91
    • My Itch page
Help - Illegal function call on a simple if
« on: February 24, 2021, 06:00:19 am »
Hi All

I guess I am missing something obvious but I must be going code blind.

I am getting a runtime error #5 illegal function call on line 39 which is just "        IF chg = 1 THEN   " in the YesNoBox function.

I use the same logic in the other function TextInputBox in this test program without error.

I am using version 1.5

Brian ...

Code: QB64: [Select]
  1.  
  2. CONST qbWHITE = _RGB32(255)
  3. CONST qbGREY = _RGB32(127)
  4. CONST qbBLACK = _RGB32(0)
  5. CONST qbDARKGREY = _RGB32(67, 67, 67)
  6. CONST qbORANGE = _RGB32(255, 128, 0)
  7. CONST qbYELLOW = _RGB32(255, 255, 0)
  8. CONST qbLIGHTGREY = _RGB32(64)
  9. CONST qbCHARS = "12345d67890-=qwertyuiop[]asdfghjkl;#\zxcvbnm,./QWERTYUIOPASDFGHJKLZXCVBNM!$%^&*()_+{}:@~|<>? "
  10.  
  11.  
  12. SCREEN _NEWIMAGE(800, 600, 32)
  13.  
  14. test = TextInputBox(200, 200, "some text", "")
  15.  
  16. PRINT YesNoBox(100, 100, "test")
  17.  
  18.  
  19.  
  20. FUNCTION YesNoBox$ (xPos AS INTEGER, yPos AS INTEGER, promptText AS STRING)
  21.     DIM char AS STRING
  22.     DIM chg AS INTEGER
  23.  
  24.     _KEYCLEAR
  25.     chg = 0
  26.     DO
  27.         char = INKEY$
  28.  
  29.         IF char <> YesNoBox THEN
  30.             chg = 1
  31.             YesNoBox = char
  32.         ELSEIF ASC(char) = 8 THEN 'backspace
  33.             YesNoBox = ""
  34.             chg = 1
  35.         END IF
  36.  
  37.         IF chg = 1 THEN
  38.             LINE (xPos, yPos)-(xPos + LEN(promptText) * 17, yPos + 50), qbLIGHTGREY, BF
  39.             LINE (xPos, yPos)-(xPos + LEN(promptText) * 17, yPos + 50), qbWHITE, B
  40.             _PRINTSTRING (xPos + 2, yPos + 5), promptText
  41.             _PRINTSTRING (xPos + 2, yPos + 30), YesNoBox
  42.             _DISPLAY
  43.         END IF
  44.  
  45.         chg = 0
  46.  
  47.  
  48.     LOOP UNTIL UCASE$(YesNoBox) = "Y" OR UCASE$(YesNoBox) = "N"
  49.  
  50.  
  51. FUNCTION TextInputBox$ (xPos AS INTEGER, yPos AS INTEGER, promptText AS STRING, defaultInput AS STRING)
  52.  
  53.     DIM charCount AS INTEGER
  54.     DIM char AS STRING
  55.     DIM chg AS INTEGER
  56.     charCount = 1
  57.     chg = 0
  58.     TextInputBox = defaultInput
  59.  
  60.     _KEYCLEAR
  61.     DO
  62.         char = INKEY$
  63.  
  64.         IF INSTR(qbCHARS, char) THEN
  65.             TextInputBox = TextInputBox + char
  66.             chg = 1
  67.         ELSEIF ASC(char) = 8 THEN
  68.             TextInputBox = LEFT$(TextInputBox, LEN(TextInputBox) - 1)
  69.             chg = 1
  70.         END IF
  71.         IF chg = 1 THEN
  72.             LINE (xPos, yPos)-(xPos + 300, yPos + 50), qbLIGHTGREY, BF
  73.             LINE (xPos, yPos)-(xPos + 300, yPos + 50), qbWHITE, B
  74.             _PRINTSTRING (xPos + 2, yPos + 5), promptText
  75.             _PRINTSTRING (xPos + 2, yPos + 30), TextInputBox
  76.             _DISPLAY
  77.         END IF
  78.         chg = 0
  79.     LOOP UNTIL _KEYDOWN(13)
  80.     _KEYCLEAR
  81.  

  [ You are not allowed to view this attachment ]  
Brian ...

Marked as best answer by 191Brian on February 24, 2021, 05:57:48 am

FellippeHeitor

  • Guest
Re: Help - Illegal function call on a simple if
« Reply #1 on: February 24, 2021, 07:40:44 am »
I don't know why it's picking the wrong line, but the error is happening in the line that checks ELSEIF ASC(char) = 8 THEN. ASC will fail if the string passed to it is empty, which will happen immediately in your code because char = INKEY$ and when the procedure is started, no key has been pressed yet. Use this change instead:

Code: QB64: [Select]
  1.  
  2. Const qbWHITE = _RGB32(255)
  3. Const qbGREY = _RGB32(127)
  4. Const qbBLACK = _RGB32(0)
  5. Const qbDARKGREY = _RGB32(67, 67, 67)
  6. Const qbORANGE = _RGB32(255, 128, 0)
  7. Const qbYELLOW = _RGB32(255, 255, 0)
  8. Const qbLIGHTGREY = _RGB32(64)
  9. Const qbCHARS = "12345d67890-=qwertyuiop[]asdfghjkl;#\zxcvbnm,./QWERTYUIOPASDFGHJKLZXCVBNM!$%^&*()_+{}:@~|<>? "
  10.  
  11.  
  12. Screen _NewImage(800, 600, 32)
  13.  
  14. test = TextInputBox(200, 200, "some text", "")
  15.  
  16. Print YesNoBox(100, 100, "test")
  17.  
  18.  
  19.  
  20. Function YesNoBox$ (xPos As Integer, yPos As Integer, promptText As String)
  21.     Dim char As String
  22.     Dim chg As Integer
  23.  
  24.     _KeyClear
  25.     chg = 0
  26.     Do
  27.         char = InKey$
  28.  
  29.         If char <> YesNoBox Then
  30.             chg = 1
  31.             YesNoBox = char
  32.         ElseIf char = Chr$(8) Then 'backspace
  33.             YesNoBox = ""
  34.             chg = 1
  35.         End If
  36.  
  37.         If chg = 1 Then
  38.             Line (xPos, yPos)-(xPos + Len(promptText) * 17, yPos + 50), qbLIGHTGREY, BF
  39.             Line (xPos, yPos)-(xPos + Len(promptText) * 17, yPos + 50), qbWHITE, B
  40.             _PrintString (xPos + 2, yPos + 5), promptText
  41.             _PrintString (xPos + 2, yPos + 30), YesNoBox
  42.             _Display
  43.         End If
  44.  
  45.         chg = 0
  46.  
  47.  
  48.     Loop Until UCase$(YesNoBox) = "Y" Or UCase$(YesNoBox) = "N"
  49.  
  50.  
  51. Function TextInputBox$ (xPos As Integer, yPos As Integer, promptText As String, defaultInput As String)
  52.  
  53.     Dim charCount As Integer
  54.     Dim char As String
  55.     Dim chg As Integer
  56.     charCount = 1
  57.     chg = 0
  58.     TextInputBox = defaultInput
  59.  
  60.     _KeyClear
  61.     Do
  62.         char = InKey$
  63.  
  64.         If InStr(qbCHARS, char) Then
  65.             TextInputBox = TextInputBox + char
  66.             chg = 1
  67.         ElseIf char = Chr$(8) Then
  68.             TextInputBox = Left$(TextInputBox, Len(TextInputBox) - 1)
  69.             chg = 1
  70.         End If
  71.         If chg = 1 Then
  72.             Line (xPos, yPos)-(xPos + 300, yPos + 50), qbLIGHTGREY, BF
  73.             Line (xPos, yPos)-(xPos + 300, yPos + 50), qbWHITE, B
  74.             _PrintString (xPos + 2, yPos + 5), promptText
  75.             _PrintString (xPos + 2, yPos + 30), TextInputBox
  76.             _Display
  77.         End If
  78.         chg = 0
  79.     Loop Until _KeyDown(13)
  80.     _KeyClear
  81.  
  82.  

Offline 191Brian

  • Newbie
  • Posts: 91
    • My Itch page
Re: Help - Illegal function call on a simple if
« Reply #2 on: February 24, 2021, 10:57:31 am »
Thank you Fellippe I went around in circles trying to find the problem.

Brian ...
I don't know why it's picking the wrong line, but the error is happening in the line that checks ELSEIF ASC(char) = 8 THEN. ASC will fail if the string passed to it is empty, which will happen immediately in your code because char = INKEY$ and when the procedure is started, no key has been pressed yet. Use this change instead:

Code: QB64: [Select]
  1.  
  2. Const qbWHITE = _RGB32(255)
  3. Const qbGREY = _RGB32(127)
  4. Const qbBLACK = _RGB32(0)
  5. Const qbDARKGREY = _RGB32(67, 67, 67)
  6. Const qbORANGE = _RGB32(255, 128, 0)
  7. Const qbYELLOW = _RGB32(255, 255, 0)
  8. Const qbLIGHTGREY = _RGB32(64)
  9. Const qbCHARS = "12345d67890-=qwertyuiop[]asdfghjkl;#\zxcvbnm,./QWERTYUIOPASDFGHJKLZXCVBNM!$%^&*()_+{}:@~|<>? "
  10.  
  11.  
  12. Screen _NewImage(800, 600, 32)
  13.  
  14. test = TextInputBox(200, 200, "some text", "")
  15.  
  16. Print YesNoBox(100, 100, "test")
  17.  
  18.  
  19.  
  20. Function YesNoBox$ (xPos As Integer, yPos As Integer, promptText As String)
  21.     Dim char As String
  22.     Dim chg As Integer
  23.  
  24.     _KeyClear
  25.     chg = 0
  26.     Do
  27.         char = InKey$
  28.  
  29.         If char <> YesNoBox Then
  30.             chg = 1
  31.             YesNoBox = char
  32.         ElseIf char = Chr$(8) Then 'backspace
  33.             YesNoBox = ""
  34.             chg = 1
  35.         End If
  36.  
  37.         If chg = 1 Then
  38.             Line (xPos, yPos)-(xPos + Len(promptText) * 17, yPos + 50), qbLIGHTGREY, BF
  39.             Line (xPos, yPos)-(xPos + Len(promptText) * 17, yPos + 50), qbWHITE, B
  40.             _PrintString (xPos + 2, yPos + 5), promptText
  41.             _PrintString (xPos + 2, yPos + 30), YesNoBox
  42.             _Display
  43.         End If
  44.  
  45.         chg = 0
  46.  
  47.  
  48.     Loop Until UCase$(YesNoBox) = "Y" Or UCase$(YesNoBox) = "N"
  49.  
  50.  
  51. Function TextInputBox$ (xPos As Integer, yPos As Integer, promptText As String, defaultInput As String)
  52.  
  53.     Dim charCount As Integer
  54.     Dim char As String
  55.     Dim chg As Integer
  56.     charCount = 1
  57.     chg = 0
  58.     TextInputBox = defaultInput
  59.  
  60.     _KeyClear
  61.     Do
  62.         char = InKey$
  63.  
  64.         If InStr(qbCHARS, char) Then
  65.             TextInputBox = TextInputBox + char
  66.             chg = 1
  67.         ElseIf char = Chr$(8) Then
  68.             TextInputBox = Left$(TextInputBox, Len(TextInputBox) - 1)
  69.             chg = 1
  70.         End If
  71.         If chg = 1 Then
  72.             Line (xPos, yPos)-(xPos + 300, yPos + 50), qbLIGHTGREY, BF
  73.             Line (xPos, yPos)-(xPos + 300, yPos + 50), qbWHITE, B
  74.             _PrintString (xPos + 2, yPos + 5), promptText
  75.             _PrintString (xPos + 2, yPos + 30), TextInputBox
  76.             _Display
  77.         End If
  78.         chg = 0
  79.     Loop Until _KeyDown(13)
  80.     _KeyClear
  81.  
  82.  
Brian ...