Author Topic: File INPUT in a sub error ??  (Read 1386 times)

0 Members and 1 Guest are viewing this topic.

Offline 191Brian

  • Newbie
  • Posts: 91
    • My Itch page
File INPUT in a sub error ??
« on: February 06, 2021, 03:38:44 pm »
Hi

I am trying the read input from a file in a subroutine, the IDE  says invalid expression on the INPUT line but if I comment out the SUB & END SUB lines
the IDE is happy with the same line.
I also note the IDE does not autocorrect the the case of a type variable array name when you refer the the array name only in a UBOUND.

Am I missing something or is file input only allowed in the mail program?

Brian ...


Code: QB64: [Select]
  1.  
  2.  
  3. TYPE SCORES
  4.     player AS STRING
  5.     score AS INTEGER
  6.  
  7. REDIM SHARED playerScores(10) AS SCORES ' High scores array
  8.  
  9. SUB LoadScores
  10.  
  11.     ' Load scores from file into array and sort highest to lowest
  12.  
  13.     DIM scoreFile AS LONG
  14.     DIM si AS INTEGER
  15.     DIM si2 AS INTEGER
  16.     DIM swapped AS INTEGER
  17.     DIM highScore AS STRING
  18.  
  19.     highScore = "highscore.txt"
  20.     scoreFile = FREEFILE
  21.     IF _FILEEXISTS(highScore) THEN
  22.         OPEN highScore FOR INPUT AS #scoreFile
  23.         si = 0
  24.         DO UNTIL EOF(scoreFile)
  25.             IF si > UBOUND(playerscores) THEN
  26.                 REDIM _PRESERVE playerscores(UBOUND(playerscores) + 1)
  27.             END IF
  28.         INPUT #scoreFile, playerScores(si).player, playerScores(si).score
  29.  
  30.         si = si + 1
  31.     LOOP
  32.  
  33.  
Brian ...

FellippeHeitor

  • Guest
Re: File INPUT in a sub error ??
« Reply #1 on: February 06, 2021, 03:43:19 pm »
What Steve says in the next post:
« Last Edit: February 06, 2021, 03:44:55 pm by FellippeHeitor »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: File INPUT in a sub error ??
« Reply #2 on: February 06, 2021, 03:43:28 pm »
LINE 28:                  REDIM _PRESERVE playerScores(UBOUND(playerscores) + 1) AS SCORES

You're not telling it what to redim your variable as.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: File INPUT in a sub error ??
« Reply #3 on: February 06, 2021, 03:45:37 pm »
It has to do with using a variable-width string in your TYPE. Change the type to something like:

Code: QB64: [Select]
  1. TYPE SCORES
  2.     player AS STRING * 20
  3.     score AS INTEGER
  4.  

Change 20 to whatever max length you'll allow for player names, and that will work.

I'm not getting any error with the player AS STRING.  I'm just seeing the issue with  the REDIM.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

  • Guest
Re: File INPUT in a sub error ??
« Reply #4 on: February 06, 2021, 03:46:34 pm »
Already redacted even before your reply. You're with stupid.

Offline 191Brian

  • Newbie
  • Posts: 91
    • My Itch page
Re: File INPUT in a sub error ??
« Reply #5 on: February 06, 2021, 03:54:06 pm »
Hi

Thank you.

The fact it didn't need the AS SCORES outside the sub confused me.

Any comment of the array name case not being autocorrected for capitalisation in the ubound?

Brian ...
Brian ...

FellippeHeitor

  • Guest
Re: File INPUT in a sub error ??
« Reply #6 on: February 06, 2021, 03:56:14 pm »
Quote
Any comment of the array name case not being autocorrected for capitalisation in the ubound?

Press conference is over.

🤣

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: File INPUT in a sub error ??
« Reply #7 on: February 06, 2021, 04:02:34 pm »
Hi

Thank you.

The fact it didn't need the AS SCORES outside the sub confused me.

Any comment of the array name case not being autocorrected for capitalisation in the ubound?

Brian ...

That's a long term glitch that nobody has sorted out yet.  It's buried deep in the code.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline 191Brian

  • Newbie
  • Posts: 91
    • My Itch page
Re: File INPUT in a sub error ??
« Reply #8 on: February 06, 2021, 04:37:45 pm »
Oh OK thanks for letting me know.
Brian ...