Menu

Odd behaviour of INPUT

Anonymous
2016-12-03
2016-12-14
  • Anonymous

    Anonymous - 2016-12-03

    I was trying to solve a puzzle on Advent of Code’s website, by the following bit of code:

    10 ' BATH.BAS - Second puzzle part 1
    20 ' WRITTEN 3 Dec 16 JRA
    30 ' Variables
    40 ' B current number of door Button. LC Loop counter
    50 ' F$ - one of five data files (one for each code no). D$ - direction of move
    100 ’
    110 B = 5 ' This is where we start for the first file only. Other runs have
    111 ' their starting point from the preceding run.
    120 INPUT "Datafile "; F$
    130 LC = 1
    140 OPEN F$ FOR RANDOM AS #1 LEN=1
    145 GET #1, LC
    150 INPUT #1, D$
    160 PRINT D$

    This is as far as I had got with the coding, (the file F$ exists) when the error arose, as follows:

    150 INPUT #1, D$
    pcbasic.statements:1979, exec_input
    None
    pcbasic.devices:403, read_var
    None
    pcbasic.devices:423, _input_entry
    None
    pcbasic.devices:411, _skip_whitespace
    None
    TypeError: 'in <string>' requires string as left operand, not bytearray

    Would it be possible to enhance PC-BASIC with such matters as having longer allowable string lengths? Relaxing the present restriction to 255 characters would not hurt the running of old programs, but would help those using an old language for new programming - I need to read in over 500 characters!

    Many thanks

     
  • Rob Hagemans

    Rob Hagemans - 2016-12-03

    Hi, that looks like bug, I'll investigate - thanks for the report!

    Unfortunately it is not possible to have strings longer than 255 characters, for the simple reason that GW-BASIC strings are stored with only a single-byte field for the length. Therefor ethe length can only be a one-byte integer, i.e. between zero and 255. Changing this would have knock-on effects basically everywhere in the interpreter, and would break old programs that depend on strings being stored in the GW-BASIC format.

    Rob

     
  • Rob Hagemans

    Rob Hagemans - 2016-12-12

    This bug is fixed by commit 49058c3, will be in next release.

     
  • Rob Hagemans

    Rob Hagemans - 2016-12-12

    This code still throws a Field Overflow in 150 after the fix, by the way - not entirely sure why, but it does the same thing in GW-BASIC so it's not a bug. I think the way to access the record is with

    142 FIELD#1, 1 AS D$
    

    and getting rid of line 150.

     
  • Anonymous

    Anonymous - 2016-12-13

    Hello Rob. Thanks for your work. I think "Field overflow" is correct - since the GET in 145 reads in the first character of the data, and then the INPUT in 150 presumably tries to address the same thing. Could it have anything to do with where the pointer is in the field?

    I will look forward to the new release.

     
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.