No announcement yet.

Why no string error here?

  • Filter
  • Time
  • Show
Clear All
new posts

  • Why no string error here?

    I would have thought that the following would generate
    a string error of some kind when it 'runs off the end.'

    WHILE PRPT% < 80
    LNPT$ = "Now is the time for all good men."
       WHILE MID$(LNPT$, PRPT% + 1, 1) <> " "'    Test for star
          PRPT% = PRPT% + 1 '     Increment cursor
          ' code snipped here which keeps the result 
          ' below always positioned so it is on screen ..
          PRINT MID$(LNPT$, PRPT%, 1); '  Reprint what was ther
    But lo! The Eveready Bunny is in the thicket! The error
    that the PB IDE finally collapses into is when PRPT% runs
    over the top of the integer allowable cap > 32767 of all
    things, that's where this collapses!

    He just keeps going and going and going ..


    Surely PRPT% isn't Siamese twin variables, no?

    Mike Luther
    [email protected]
    Mike Luther
    [email protected]

  • #2
    No, there is no problem with the compiler, but your code contains a couple of logic errors that prevent it working successfully in all circumstances.

    Basically, the MID$ function generates no run-time errors if the string length is less than the position being tested (or inserted as would be the case with the MID$ statement).

    If you think about it, why would MID$ need to return an error? It would make it much harder to have to write an error trap to handle a "logic error" such as your code demonstrates.

    In summary, it is the programmers responsibility to write code that does not contain "logic errors" or makes "unsafe assumptions" about the data being processed.

    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    mailto:[email protected]


    • #3
      Yes, there is a bug in the code, but the answer to the question is, "Because MID$(string, positionbeyondlengthofstring) returns NULL, which does not equal SPACE, which is why the loop keeps executing.".

      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]