Menu

Randomize for next & Reading from sequential file

2018-11-17
2018-11-19
  • Richard Couture

    Richard Couture - 2018-11-17

    I do not understand why in the following short For Next program after 16, the program goes on and on and on. Any indicator would be appreciated:

    10 CLE = 15
    20 FOR I = 1 TO CLE +1
    30 I =INT(RND*CLE)
    40 PRINT I
    50 NEXT

    In addition, is it possible to specify the line i want to input from a sequential file

    Thank you

    Richard

     
  • Rob Hagemans

    Rob Hagemans - 2018-11-17

    The program is changing the loop variable I to a value less than CLE+1, since RND yield a value between 0 and 1. Since the loop condition is never reached, the loop does not terminate.

    You can't specify which line to input from a sequential file, you have to input all lines sequentially.

     
  • Richard Couture

    Richard Couture - 2018-11-18

    Thank you very much, so how could i can i change it so that it pick up a number between 1 to 16 and then pas it to my input statement so that it read that specific number record.

    Richard

     
  • Rob Hagemans

    Rob Hagemans - 2018-11-18

    Sorry, but I'm not sure I understand what you're trying to achieve here, in particular with the FOR loop and the random number, and where in all this reading files comes in.

    However, if you want to read specific records by numbers, you should work with random-access files. The tutorial by Steve Estvanik at http://www.o-bizz.de/qbtuts/gw-train/#6 has a good introduction on how to work with them.

     
  • Richard Couture

    Richard Couture - 2018-11-18

    Hi ! this is little project to keep my mind up to speed.
    I have a sequential file wich have 16 records. Each record has 6 characters.
    I want to select the record randomly then write them in a random file.
    After that, i will choose randomly one character from the string and display it.
    I hope this clarify my request.

    Richard

     
    • Rob Hagemans

      Rob Hagemans - 2018-11-19

      OK, well in the above code I would start by making sure the loop variable is not being changed by the code in the loop, i.e. if you're going to use I for your record number your loop would have to run over another variable, maybe K. Hope that helps. Do check out the tutorial on random and sequential files as well.

       
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.