Author Topic: help my  (Read 41478 times)

0 Members and 1 Guest are viewing this topic.

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
help my
« on: June 30, 2020, 05:22:57 pm »

hello
I don't know if this is the right place to ask for the solution
I have a problem to solve my program created in QB64.
reads the .dat file only forward
clicking with the button +
works well scrolls the list inside the file
I need some instruction that when I click the button -   go back to the file list
what instruction should i use?

I leave here the listing and also the .dat file to read
I hope some good soul can help me
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: help my
« Reply #1 on: June 30, 2020, 05:24:31 pm »
file .dat
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: help my
« Reply #2 on: June 30, 2020, 09:24:40 pm »
To go up and down a file listing, it would be best to load the file lines into an array.

It is allot easier to go up and down the lines of an array, in fact you can jump from one line to any other as easy as going up and down, if you use an array.

Welcome to the forum Kiara87, do you know about arrays?

When you open a file for INPUT you can only read down it, ... wait a second I did this the hard way, like what you are trying. I will try and dig up my code...  maybe we can save lessons on arrays for later! :-))


Oh you are using an array :P this is simple fix... 

oh no, "a" is just a string.

Working....




« Last Edit: June 30, 2020, 10:00:52 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: help my
« Reply #3 on: June 30, 2020, 10:18:09 pm »
I hope I did this in a way you can follow and I did it without arrays!

Code: QB64: [Select]
  1. OPTION _EXPLICIT 'for typo's   yep lineMun blah!
  2.  
  3. DIM a AS STRING, count AS INTEGER, lineNum AS INTEGER, Q$, num AS INTEGER
  4.  
  5. 'count lines in file
  6. OPEN "lotto.dat" FOR INPUT AS #1
  7.     INPUT #1, a
  8.     'PRINT a
  9.     'INPUT " OK enter "; w$
  10.     IF LEN(_TRIM$(a)) THEN count = count + 1
  11. 'CLS: PRINT count '480 lines
  12. 'END
  13. 'CLS
  14.  
  15. LOCATE 1, 1
  16. PRINT "BARI"
  17. LOCATE 3, 1
  18. PRINT "CAGLIARI"
  19. LOCATE 5, 1
  20. PRINT "FIRENZE"
  21. LOCATE 7, 1
  22. PRINT "GENOVA"
  23. LOCATE 9, 1
  24. PRINT "MILANO"
  25. LOCATE 11, 1
  26. PRINT "NAPOLI"
  27. LOCATE 13, 1
  28. PRINT "PALERMO"
  29. LOCATE 15, 1
  30. PRINT "ROMA"
  31. LOCATE 17, 1
  32. PRINT "TORINO"
  33. LOCATE 19, 1
  34. PRINT "VENEZIA"
  35. LOCATE 21, 1
  36. PRINT "NAZIONALE"
  37.  
  38.  
  39. LOCATE 23, 1: COLOR 4
  40. PRINT " + to move on"
  41. LOCATE 23, 16: COLOR 3
  42. PRINT "- to back"
  43.  
  44. lineNum = 1
  45. GOSUB get_a
  46.  
  47.  
  48.  
  49.     Q$ = INKEY$
  50.     IF Q$ = "+" THEN lineNum = lineNum + 1
  51.     IF lineNum > count THEN lineNum = 1
  52.  
  53.     IF Q$ = "-" THEN lineNum = lineNum - 1
  54.     IF lineNum < 1 THEN lineNum = count
  55.  
  56.     GOSUB get_a
  57.  
  58.  
  59.     LOCATE 1, 34
  60.     PRINT "estraz.num "; LEFT$(a, 4); "   lineNum:"; STR$(lineNum)
  61.  
  62.     REM bari
  63.     LOCATE 1, 13
  64.     PRINT VAL(MID$(a, 5, 2))
  65.     LOCATE 1, 16
  66.     PRINT VAL(MID$(a, 7, 2))
  67.     LOCATE 1, 19
  68.     PRINT VAL(MID$(a, 9, 2))
  69.     LOCATE 1, 22
  70.     PRINT VAL(MID$(a, 11, 2))
  71.     LOCATE 1, 25
  72.     PRINT VAL(MID$(a, 13, 2))
  73.  
  74.     REM cagliari
  75.     LOCATE 3, 13
  76.     PRINT VAL(MID$(a, 15, 2))
  77.     LOCATE 3, 16
  78.     PRINT VAL(MID$(a, 17, 2))
  79.     LOCATE 3, 19
  80.     PRINT VAL(MID$(a, 19, 2))
  81.     LOCATE 3, 22
  82.     PRINT VAL(MID$(a, 21, 2))
  83.     LOCATE 3, 25
  84.     PRINT VAL(MID$(a, 23, 2))
  85.  
  86.     REM firenze
  87.     LOCATE 5, 13
  88.     PRINT VAL(MID$(a, 25, 2))
  89.     LOCATE 5, 16
  90.     PRINT VAL(MID$(a, 27, 2))
  91.     LOCATE 5, 19
  92.     PRINT VAL(MID$(a, 29, 2))
  93.     LOCATE 5, 22
  94.     PRINT VAL(MID$(a, 31, 2))
  95.     LOCATE 5, 25
  96.     PRINT VAL(MID$(a, 33, 2))
  97.  
  98.     REM genova
  99.     LOCATE 7, 13
  100.     PRINT VAL(MID$(a, 35, 2))
  101.     LOCATE 7, 16
  102.     PRINT VAL(MID$(a, 37, 2))
  103.     LOCATE 7, 19
  104.     PRINT VAL(MID$(a, 39, 2))
  105.     LOCATE 7, 22
  106.     PRINT VAL(MID$(a, 41, 2))
  107.     LOCATE 7, 25
  108.     PRINT VAL(MID$(a, 43, 2))
  109.  
  110.     REM milano
  111.     LOCATE 9, 13
  112.     PRINT VAL(MID$(a, 45, 2))
  113.     LOCATE 9, 16
  114.     PRINT VAL(MID$(a, 47, 2))
  115.     LOCATE 9, 19
  116.     PRINT VAL(MID$(a, 49, 2))
  117.     LOCATE 9, 22
  118.     PRINT VAL(MID$(a, 51, 2))
  119.     LOCATE 9, 25
  120.     PRINT VAL(MID$(a, 53, 2))
  121.  
  122.     REM napoli
  123.     LOCATE 11, 13
  124.     PRINT VAL(MID$(a, 55, 2))
  125.     LOCATE 11, 16
  126.     PRINT VAL(MID$(a, 57, 2))
  127.     LOCATE 11, 19
  128.     PRINT VAL(MID$(a, 59, 2))
  129.     LOCATE 11, 22
  130.     PRINT VAL(MID$(a, 61, 2))
  131.     LOCATE 11, 25
  132.     PRINT VAL(MID$(a, 63, 2))
  133.  
  134.     REM palermo
  135.     LOCATE 13, 13
  136.     PRINT VAL(MID$(a, 65, 2))
  137.     LOCATE 13, 16
  138.     PRINT VAL(MID$(a, 67, 2))
  139.     LOCATE 13, 19
  140.     PRINT VAL(MID$(a, 69, 2))
  141.     LOCATE 13, 22
  142.     PRINT VAL(MID$(a, 71, 2))
  143.     LOCATE 13, 25
  144.     PRINT VAL(MID$(a, 73, 2))
  145.  
  146.     REM roma
  147.     LOCATE 15, 13
  148.     PRINT VAL(MID$(a, 75, 2))
  149.     LOCATE 15, 16
  150.     PRINT VAL(MID$(a, 77, 2))
  151.     LOCATE 15, 19
  152.     PRINT VAL(MID$(a, 79, 2))
  153.     LOCATE 15, 22
  154.     PRINT VAL(MID$(a, 81, 2))
  155.     LOCATE 15, 25
  156.     PRINT VAL(MID$(a, 83, 2))
  157.  
  158.     REM torino
  159.     LOCATE 17, 13
  160.     PRINT VAL(MID$(a, 85, 2))
  161.     LOCATE 17, 16
  162.     PRINT VAL(MID$(a, 87, 2))
  163.     LOCATE 17, 19
  164.     PRINT VAL(MID$(a, 89, 2))
  165.     LOCATE 17, 22
  166.     PRINT VAL(MID$(a, 91, 2))
  167.     LOCATE 17, 25
  168.     PRINT VAL(MID$(a, 93, 2))
  169.  
  170.     REM venezia
  171.     LOCATE 19, 13
  172.     PRINT VAL(MID$(a, 95, 2))
  173.     LOCATE 19, 16
  174.     PRINT VAL(MID$(a, 97, 2))
  175.     LOCATE 19, 19
  176.     PRINT VAL(MID$(a, 99, 2))
  177.     LOCATE 19, 22
  178.     PRINT VAL(MID$(a, 101, 2))
  179.     LOCATE 19, 25
  180.     PRINT VAL(MID$(a, 103, 2))
  181.  
  182.     REM nazionale
  183.     LOCATE 21, 13
  184.     PRINT VAL(MID$(a, 105, 2))
  185.     LOCATE 21, 16
  186.     PRINT VAL(MID$(a, 107, 2))
  187.     LOCATE 21, 19
  188.     PRINT VAL(MID$(a, 109, 2))
  189.     LOCATE 21, 22
  190.     PRINT VAL(MID$(a, 111, 2))
  191.     LOCATE 21, 25
  192.     PRINT VAL(MID$(a, 113, 2))
  193.  
  194.     _LIMIT 30
  195.  
  196.  
  197. 'this sub retrieves a line number from file  the hard way
  198. get_a:
  199. num = 0
  200. OPEN "lotto.dat" FOR INPUT AS #1
  201.     INPUT #1, a
  202.     IF LEN(_TRIM$(a)) THEN num = num + 1
  203.     IF num = lineNum THEN EXIT WHILE 'a is set on line we want
  204.  
  205.  
  206.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: help my
« Reply #4 on: June 30, 2020, 11:46:35 pm »
Code: QB64: [Select]
  1. LP 7, 20, "One of the first SUBs I ever wrote was LP"
  2.  
  3. LP 9, 15, "L is for LOCATE"
  4. LP 10, 15, "P is for PRINT"
  5.  
  6. LP 12, 15, "It saved me all kinds of time typing LOCATE: aRow, aCol"
  7. LP 13, 15, "Then printing: PRINT mystr$."
  8.  
  9. LP 15, 15, "So now I am sharing it with you"
  10. LP 16, 15, "so you can save yourself some typing   and then"
  11. LP 17, 15, "you can share it with someone new."
  12.  
  13. ' L is for LOCATE P is for PRINT together they save you a whole lot of typing!
  14. SUB LP (row, column, Strng$)
  15.     LOCATE row, column: PRINT Strng$
  16.  
  17.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: help my
« Reply #5 on: July 01, 2020, 12:05:15 am »
Just curious, but can't you use _PRINTSTRING instead of LP?
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: help my
« Reply #6 on: July 01, 2020, 02:15:12 am »
Just curious, but can't you use _PRINTSTRING instead of LP?

Not in 1991!

Of course I could use it now but then I'd have to explain _NEWIMAGE for graphics. Not sure the OP is ready for that.

FellippeHeitor

  • Guest
Re: help my
« Reply #7 on: July 01, 2020, 02:18:25 am »
I have a problem to solve my program created in QB64.

Learning the syntax for SUB LP vs learning the syntax for _PRINTSTRING... I'm with Steve on this one 😉

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: help my
« Reply #8 on: July 01, 2020, 02:26:39 am »
And _PRINTSTRING works in legacy modes.  (Even SCREEN 0.)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: help my
« Reply #9 on: July 01, 2020, 02:32:45 am »
Yeah, OK your blowing my nostalgic moment :P

BTW this is how I learned SUB's back in the day.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: help my
« Reply #10 on: July 01, 2020, 07:02:14 am »
In bplus' defense, LP is a lot quicker to type than _PRINTSTRING.

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: help my
« Reply #11 on: July 01, 2020, 07:45:01 am »
thank bplus you very kind I am really grateful

thanks also to all of you who answered

bplus I wanted to do with arrays

but still they are beginners

I'm a beginner

thanks to this forum I will try to learn
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: help my
« Reply #12 on: July 01, 2020, 09:25:45 am »
bplus thanks
i tried to put with arrays
i'm going crazy you can give me a tip of how i can do it
also to improve it because I have to make it sum the numbers and I think it takes more variables to do this

example of sum would be so?
MID$(a, 5, 2) > 30  MID$(a, 15, 2) then color 4
but the variable a it is of type string it cannot be added up
any suggestions?
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: help my
« Reply #13 on: July 01, 2020, 09:36:04 am »
bplus thanks
i tried to put with arrays
i'm going crazy you can give me a tip of how i can do it
also to improve it because I have to make it sum the numbers and I think it takes more variables to do this

example of sum would be so?
MID$(a, 5, 2) > 30  MID$(a, 15, 2) then color 4
but the variable a it is of type string it cannot be added up
any suggestions?

2nd thing first :)  assuming you want to add 30 to the 2nd VAL(MID$(a, 5, 2)

IF VAL(MID$(a, 5, 2)) > 30 +  VAL(MID$(a, 5, 2) THEN COLOR 4

VAL() changes string type to number for as far left to right as there are numbers.
eg VAL("123ABC") = 123

For arrays, I am afraid if I do it for you it will take you longer to learn. Can you show me what you've tried?
I would love to rewrite this program with an array :)

You will love how arrays make job easy if you can just get use to them.

PS sometimes when describing what you are having problem with to another person suddenly makes clear what you need to do, sometimes you just plain get stuck!
« Last Edit: July 01, 2020, 09:51:10 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: help my
« Reply #14 on: July 01, 2020, 10:56:42 am »
BTW there are more advanced ways to access a file that give up and down and all around flexibility for access but that is not a place to start. OPEN FOR INPUT is classic way to start learning file access. There are more flexible arrays that are started with REDIM, but again, that is not classic way to start learning arrays.

Question #1 about arrays, how big?

Because before you can use an array you have to DIM it (exceptions to almost every rule like if array size <=10 you don't have to DIM but I would anyway because you can then change to bigger by just changing number).

Think of DIM as reserving space in the computer memory bank. For variables reservations are easy they are known size but arrays? how many of these variables are wanting or be needing for the array though? You tell it through DIM statement DIM myArray$(100) that reserves space for 101 lines from 0 to 100. Now we can go from myArray$(0) to myArray$(100) without QB64 telling us our "subscript is out of bounds" referring to the number inside the parenthesis. It is usually called the index and it is usually given the variable name i, i for index.

OK so an array for our file, how big is that going to have to be? Most times we don't know and we just count the lines first like I did, so I knew not to go past a certain line number in file.

As usual with QB64 there are other ways to approach loading a file with unknown size into an array but let's stick with classic way from QB upon which QB64 was based, actually from GW BASIC upon which QB was based.

So enough lecture:
Code: QB64: [Select]
  1. 'count lines in file
  2. OPEN "lotto.dat" FOR INPUT AS #1
  3.     INPUT #1, a
  4.     'PRINT a
  5.     'INPUT " OK enter "; w$
  6.     IF LEN(_TRIM$(a)) THEN count = count + 1  'make sure we count real lines with stuff AKA contents

'set the size of the array
Code: QB64: [Select]
  1. DIM myArray$(count) ' we will leave myArray$(0) empty so that the Upper Bound of array matches the number of lines with stuff in them

Is that enough of a hint? Do you have that already?

Can you guess the next step: How to load the array? We practically have the code already! just add copy/paste and add or change one line.





« Last Edit: July 01, 2020, 11:01:48 am by bplus »