Author Topic: random file puts only one record in file  (Read 3366 times)

0 Members and 1 Guest are viewing this topic.

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
random file puts only one record in file
« on: October 01, 2020, 04:59:13 pm »
Hello

i am banging my head on the desk LOL i cant get this random file to insert more than one record

i use a lot of rem statements i am disabled and need this system to help me look at the code.

i use gosubs and goto a lot need to get this done will rewrite later after it is being use so i can use subs and
functions
with that said here is the code


Code: QB64: [Select]
  1. REM ***************************************************************************
  2. REM **** Program Bridal Store *************************************************
  3. REM ***************************************************************************
  4. REM ***************************************************************************
  5. REM **** Author Philip King  **************************************************
  6. REM ***************************************************************************
  7. REM ***************************************************************************
  8. REM ***************************************************************************
  9. REM **** Includes go here *****************************************************
  10. REM ***************************************************************************
  11.  
  12. REM ***************************************************************************
  13. REM ***************************************************************************
  14. REM **** Sreen statements here ************************************************
  15. REM ***************************************************************************
  16.  
  17.  
  18. REM ***************************************************************************
  19.  
  20. REM ***************************************************************************
  21. REM **** defines for quick use data types *************************************
  22. REM ***************************************************************************
  23.  
  24. REM ***************************************************************************
  25.  
  26. REM ***************************************************************************
  27. REM **** Listing of subs and funtions *****************************************
  28. REM ***************************************************************************
  29.  
  30. REM ***************************************************************************
  31.  
  32. REM ***************************************************************************
  33. REM **** arrays decleared here ************************************************
  34. REM ***************************************************************************
  35. imenu = 4
  36. DIM SHARED smenu(imenu) AS STRING
  37.  
  38. REM ***************************************************************************
  39.  
  40. REM ***************************************************************************
  41. REM **** arrays set to data here **********************************************
  42. REM ***************************************************************************
  43.  
  44. smenu(1) = "1) Edit Customer Info": smenu(2) = "3) Edit Inventory Data "
  45. smenu(3) = "2) Edit Sales ": smenu(4) = "4) System Maintenance "
  46.  
  47. REM ***************************************************************************
  48. REM **** variables declares go here *******************************************
  49. REM ***************************************************************************
  50. CONST null = ""
  51. REM ***************************************************************************
  52.  
  53. REM ***************************************************************************
  54. REM **** customer info record type ********************************************
  55. REM ***************************************************************************
  56. TYPE customer
  57.         id AS STRING * 10
  58.         sfirst_name AS STRING * 25
  59.         slast_name AS STRING * 25
  60.         saddress AS STRING * 30
  61.         scity AS STRING * 25
  62.         sstate AS STRING * 2
  63.         szip AS STRING * 5
  64.         sphone AS STRING * 10
  65.         sfiller AS STRING * 124
  66. REM ***************************************************************************
  67.  
  68. REM ***************************************************************************
  69. REM **** inventory record type ************************************************
  70. REM ***************************************************************************
  71. TYPE inventory
  72.         ienventory AS STRING * 10
  73.         sdesc AS STRING * 250
  74.         sdate_bought AS STRING * 8
  75.         sdate_sold AS STRING * 8
  76.         fcost AS _FLOAT
  77.         fretail AS _FLOAT
  78.         ssize AS STRING * 2
  79.         ssold AS STRING * 1
  80.  
  81. REM ***************************************************************************
  82.  
  83. REM ***************************************************************************
  84. REM **** system data file record **********************************************
  85. REM ***************************************************************************
  86. TYPE systemdat
  87.         last_rec AS STRING * 9
  88.         system_data_path AS STRING * 25
  89.         customer_db AS STRING * 25
  90.         inventory_db AS STRING * 25
  91.         invoice_db AS STRING * 25
  92.         sfiller AS STRING * 148
  93.  
  94. REM ***************************************************************************
  95. REM **** point of sale type ***************************************************
  96. REM ***************************************************************************
  97. TYPE spos
  98.         lposid AS LONG
  99. REM ***************************************************************************
  100. DIM SHARED custinforec AS customer
  101. DIM SHARED systemrec AS systemdat
  102. lcustreclen = LEN(custinforec)
  103. lsystemreclen = LEN(systemrec)
  104.  
  105. 'PRINT lsystemreclen
  106.  
  107. 'END
  108.  
  109. GOTO main
  110.  
  111.  
  112. REM ***************************************************************************
  113. REM **** main menu loop *******************************************************
  114. REM ***************************************************************************
  115.  
  116. menuloop:
  117. GOSUB systemheader
  118. GOSUB printmain
  119. GOSUB mainmenuchoice
  120. IF sr = "" OR sr = CHR$(13) THEN GOTO menuloop
  121. IF sr = "X" THEN END
  122. IF sr = "1" THEN
  123.         CLS
  124.         GOSUB systemheader
  125.         GOSUB s0
  126. 'if sr="2" then gosub iv1
  127.  
  128. REM ***************************************************************************
  129.  
  130.  
  131.  
  132.  
  133. REM ***************************************************************************
  134. REM **** Sytem Header *********************************************************
  135. REM ***************************************************************************
  136. systemheader:
  137. COLOR White, Blue
  138. LOCATE 1, 1: PRINT CHR$(201): LOCATE 1, 80: PRINT CHR$(187)
  139. LOCATE 1, 2: PRINT STRING$(78, 205):
  140. FOR ix = 2 TO 7
  141.         LOCATE ix, 1: PRINT CHR$(186)
  142.         LOCATE ix, 80: PRINT CHR$(186)
  143. NEXT ix
  144. LOCATE 7, 2: PRINT STRING$(78, 205)
  145. LOCATE 7, 1: PRINT CHR$(200): LOCATE 7, 80: PRINT CHR$(188)
  146. LOCATE 4, 30: PRINT "Main System Menu"
  147. LOCATE 4, 63: PRINT "Date ": LOCATE 4, 68: PRINT DATE$
  148. LOCATE 4, 4: PRINT "Virsion 1.0"
  149.  
  150.  
  151. REM ***************************************************************************
  152. REM **** printes in system erros **********************************************
  153. REM ***************************************************************************
  154. sprintserrors:
  155. IF se = "" THEN se = "INVALID RESPONSE"
  156. itemp = LEN(se)
  157. itemp1 = 40 - (itemp / 2)
  158. COLOR 16, 7
  159. 'COLOR Yellow, Blue
  160. LOCATE 25, itemp1: _BLINK ON: PRINT se;
  161. idelay = 1.5
  162. CALL Delay(1.5)
  163. iflag2 = 1
  164.  
  165. REM ***************************************************************************
  166.  
  167. REM *****************************************************************************
  168. REM **** numeric input sub ******************************************************
  169. REM *****************************************************************************
  170. snumeric:
  171.  
  172. ftemp = VAL(sr)
  173. IF ftemp < ilow OR ftemp > ihigh THEN
  174.         se = "INPUT OUT OF BOUNDS"
  175.         GOSUB sprintserrors
  176.  
  177.  
  178. REM ***************************************************************************
  179.  
  180. REM ***************************************************************************
  181. REM **** alpha input sub ******************************************************
  182. REM ***************************************************************************
  183. salphainput:
  184.  
  185. iflag2 = 0
  186. ilength1 = LEN(sr)
  187. ilength2 = LEN(saction) + 2
  188.  
  189. LOCATE 23, 1: PRINT saction; '               prints action menu
  190. LOCATE 23, ilength2 '                        set prompt one space after action menu print
  191. COLOR Yellow, Black: PRINT SPC(ib); '                changes color and prints ib spaces to screen for input lenght
  192. LOCATE 23, ilength2 '                        sets curser at the begining of input space from previouse line
  193. LINE INPUT sr: sr = UCASE$(sr) '             inputs data from user changes it to upper case
  194.  
  195. IF LEN(sr) > ib THEN
  196.         se = "ENVALID INPUT LENGTH"
  197.         GOSUB sprintserrors
  198.         sr = ""
  199.         iflag2 = 1
  200.         RETURN
  201. IF sr = "X" AND iflag1 = 1 THEN iflag1 = 0
  202.  
  203. IF iflag1 = 1 AND VAL(sr) < ilow AND VAL(sr) > ihigh THEN GOSUB snumeric
  204.  
  205. COLOR White, Blue
  206.  
  207.  
  208. REM ***************************************************************************
  209. REM **** main menu to the screen **********************************************
  210. REM ***************************************************************************
  211. printmain:
  212. COLOR White, Blue
  213. FOR ix = 11 TO imenu
  214.         READ smenu(ix): LOCATE ix, 20: PRINT smenu(ix)
  215. NEXT ix
  216.  
  217. LOCATE 11, 15: PRINT smenu(1)
  218. LOCATE 11, 40: PRINT smenu(2)
  219. LOCATE 13, 15: PRINT smenu(3)
  220. LOCATE 13, 40: PRINT smenu(4)
  221.  
  222. REM ***************************************************************************
  223.  
  224. REM ***************************************************************************
  225. REM **** menu prompts start here **********************************************
  226. REM ***************************************************************************
  227. mainmenuchoice:
  228. ib = 1
  229. ihigh = 99
  230. ilow = 1
  231. saction = "Choose Menu Option or X to Quit "
  232. GOSUB salphainput
  233.  
  234. REM ***************************************************************************
  235.  
  236. REM ***************************************************************************
  237. REM **** clean up the prompt area *********************************************
  238. REM ***************************************************************************
  239.  
  240. clearprompt:
  241. LOCATE 23, 1: PRINT STRING$(79, 32)
  242.  
  243. REM ***************************************************************************
  244.  
  245. REM ***************************************************************************
  246. REM **** customer input modual ************************************************
  247. REM ***************************************************************************
  248. s0:
  249.  
  250. LOCATE 10, 1: PRINT "Customer ID           ";: PRINT custinforec.id
  251. LOCATE 11, 1: PRINT "Customer First Name   ";: PRINT custinforec.sfirst_name
  252. LOCATE 12, 1: PRINT "customer Last Name    ";: PRINT custinforec.slast_name
  253. LOCATE 13, 1: PRINT "Customer Address      ";: PRINT custinforec.saddress
  254. LOCATE 14, 1: PRINT "Customer City         ";: PRINT custinforec.scity
  255. LOCATE 15, 1: PRINT "customer State        ";: PRINT custinforec.sstate
  256. LOCATE 16, 1: PRINT "customer Zip          ";: PRINT custinforec.szip
  257. LOCATE 17, 1: PRINT "Customer Phone Number ";
  258. PRINT LEFT$(custinforec.sphone, 3) + "/"; MID$(custinforec.sphone, 4, 3) + "/"; RIGHT$(custinforec.sphone, 4)
  259. REM ********************************************************************
  260. s1: saction = "Enter Customer ID X to Exit "
  261. ib = 10
  262. ilow = 1
  263. ihigh = 999999999
  264. iflag1 = 0
  265. IF iflag2 = 1 THEN
  266.         GOSUB clearprompt
  267.         GOSUB salphainput
  268.         GOSUB clearprompt
  269.         GOSUB salphainput
  270.  
  271. IF sr = "X" THEN RETURN
  272. IF sr = "" OR sr = CHR$(13) GOTO s2
  273. GOSUB dupidsearch
  274. custinforec.id = sr: GOSUB s0: GOTO s2
  275.  
  276.  
  277. REM ********************************************************************
  278. s2:
  279. saction = "Enter First Name X to Exit - To Backup "
  280. ib = 25
  281. ilow = 99
  282. ihigh = 99
  283. iflag1 = 0
  284. IF iflag2 = 1 THEN
  285.         GOSUB clearprompt
  286.         GOSUB salphainput
  287.         GOSUB clearprompt
  288.         GOSUB salphainput
  289. IF sr = "-" THEN GOTO s1
  290.  
  291. IF sr = "X" THEN RETURN
  292. IF sr = "" OR sr = CHR$(13) GOTO s3
  293. custinforec.sfirst_name = sr: GOSUB s0
  294.  
  295. REM ***************************************************************************
  296. s3:
  297. saction = "Enter Last Name X to Exit - To Back up "
  298. ib = 25
  299. ilow = 99
  300. ihigh = 99
  301. iflag1 = 0
  302. IF iflag2 = 1 THEN
  303.         GOSUB clearprompt
  304.         GOSUB salphainput
  305.         GOSUB clearprompt
  306.         GOSUB salphainput
  307.  
  308. IF sr = "-" THEN GOTO s2
  309. IF sr = "X" THEN RETURN
  310. IF sr = "" OR sr = CHR$(13) GOTO s4
  311.  
  312. custinforec.slast_name = sr: GOSUB s0
  313.  
  314.  
  315. REM ***************************************************************************
  316. s4:
  317. saction = "Enter Address X to Exit - To Back up "
  318. ib = 30
  319. ilow = 99
  320. ihigh = 99
  321. iflag1 = 0
  322. IF iflag2 = 1 THEN
  323.         GOSUB clearprompt
  324.         GOSUB salphainput
  325.         GOSUB clearprompt
  326.         GOSUB salphainput
  327.  
  328. IF sr = "-" THEN GOTO s3
  329. IF sr = "X" THEN RETURN
  330. IF sr = "" OR sr = CHR$(13) GOTO s5
  331. custinforec.saddress = sr: GOSUB s0
  332.  
  333.  
  334. REM ***************************************************************************
  335. s5: saction = "Enter City Name X to Exit - To Back up "
  336. ib = 25
  337. ilow = 99
  338. ihigh = 99
  339. iflag1 = 0
  340. IF iflag2 = 1 THEN
  341.         GOSUB clearprompt
  342.         GOSUB salphainput
  343.         GOSUB clearprompt
  344.         GOSUB salphainput
  345.  
  346. IF sr = "-" THEN GOTO s4
  347. IF sr = "X" THEN RETURN
  348. IF sr = "" OR sr = CHR$(13) GOTO s6
  349. custinforec.scity = sr: GOSUB s0
  350.  
  351. REM ***************************************************************************
  352. s6:
  353. saction = "Enter State X to Exit - To Back up "
  354. ib = 2
  355. ilow = 99
  356. ihigh = 99
  357. iflag1 = 0
  358. IF iflag2 = 1 THEN
  359.         GOSUB clearprompt
  360.         GOSUB salphainput
  361.         GOSUB clearprompt
  362.         GOSUB salphainput
  363.  
  364. IF sr = "-" THEN GOTO s5
  365. IF sr = "X" THEN RETURN
  366. IF sr = "" OR sr = CHR$(13) GOTO s7
  367. custinforec.sstate = sr: GOSUB s0
  368.  
  369. REM ***************************************************************************
  370. s7:
  371. saction = "Enter Zip Code Name X to Exit - To Back up "
  372. ib = 5
  373. ilow = 99
  374. ihigh = 99
  375. iflag1 = 0
  376. IF iflag2 = 1 THEN
  377.         GOSUB clearprompt
  378.         GOSUB salphainput
  379.         GOSUB clearprompt
  380.         GOSUB salphainput
  381.  
  382. IF sr = "-" THEN GOTO s6
  383. IF sr = "X" THEN RETURN
  384.  
  385. custinforec.szip = sr: GOSUB s0
  386. REM ***************************************************************************
  387. s8:
  388. saction = "Enter Phone Number X to Exit - To Back up "
  389. ib = 10
  390. ilow = 99
  391. ihigh = 99
  392. iflag1 = 0
  393. IF iflag2 = 1 THEN
  394.         GOSUB clearprompt
  395.         GOSUB salphainput
  396.         GOSUB clearprompt
  397.         GOSUB salphainput
  398.  
  399. IF sr = "-" THEN GOTO s7
  400. IF sr = "X" THEN RETURN
  401. custinforec.sphone = sr: GOSUB s0
  402. 'IF sr = "" OR sr = CHR$(13) goto next step
  403.  
  404.  
  405. REM ***************************************************************************
  406.  
  407. REM ***************************************************************************
  408. REM **** ask if you want to put data to record ********************************
  409. REM ***************************************************************************
  410. puttorecord:
  411. saction = "Commit Data to File Y/N"
  412. ib = 1
  413. ilow = 99
  414. ihigh = 99
  415. iflag1 = 0
  416. GOSUB clearprompt
  417. GOSUB salphainput
  418. IF sr = "-" THEN GOSUB s8
  419. IF sr = "Y" THEN GOSUB insertcustrec
  420. IF sr = "N" THEN RETURN
  421. REM ***************************************************************************
  422. REM **** inventory input screen ***********************************************
  423. REM ***************************************************************************
  424. inv1:
  425. REM ***
  426. REM ***************************************************************************
  427. REM **** set custinforrec variables to zero or null ***************************
  428. REM ***************************************************************************
  429. custinforec_null:
  430. custinforec.id = null
  431. custinforec.sfirst_name = null
  432. custinforec.slast_name = null
  433. custinforec.saddress = null
  434. custinforec.scity = null
  435. custinforec.sstate = null
  436. custinforec.szip = null
  437. custinforec.sphone = null
  438. REM ***************************************************************************
  439.  
  440. REM ***************************************************************************
  441. REM **** opens random files ***************************************************
  442. REM ***************************************************************************
  443. randomfiles:
  444. OPEN "jbradal.db" FOR RANDOM AS #1 LEN = lcustreclen
  445. OPEN "system.db" FOR RANDOM AS #5 LEN = lsystemreclen
  446. zcustrecnum = SEEK(1)
  447. 'IF zcustrecnum = 0 THEN zcustrecnum = 1 '
  448.  
  449. REM ***************************************************************************
  450.  
  451. REM ***************************************************************************
  452. REM **** inserts record into random file **************************************
  453. REM ***************************************************************************
  454. insertcustrec:
  455. IF zcustrecnum = 1 OR zcustrecnum < 2 THEN
  456.  
  457.         PUT #1, zcustrecnum, custinforec
  458.         zcustrecnum = zcustrecnum + 1
  459.         PUT #1, zcustrecnum, custinforec
  460. REM ***************************************************************************
  461.  
  462. REM ***************************************************************************
  463. REM **** custinforec duplicate id search **************************************
  464. REM ***************************************************************************
  465. dupidsearch:
  466. 'IF (LOF(1) / lcustreclen) = 0 THEN RETURN
  467. 'WHILE NOT LOF(1) / lcustreclen
  468. 'GET #1, zcustrecnum * lcusinforeclen, custinforec
  469. 'IF custinforec.id = sr THEN BEEP
  470. 'WEND
  471. REM ***************************************************************************
  472.  
  473. REM ***************************************************************************
  474. REM **** main modual **********************************************************
  475. REM ***************************************************************************
  476. main:
  477. GOSUB randomfiles '
  478. 'END
  479. SCREEN _NEWIMAGE(640, 400, 32)
  480. COLOR LightCyan, MidnightBlue
  481. iflag2 = 0
  482. iflag3 = 1
  483. 'SCREEN 12
  484. IF se = "X" THEN
  485.         CLOSE
  486.         END
  487. mainloop:
  488. GOSUB menuloop:
  489. IF saction = "X" THEN
  490.         CLOSE
  491.         END
  492. IF sr = "1" THEN
  493.         GOSUB custinforec_null
  494.         GOSUB s1
  495.         GOTO mainloop
  496. 'if sr="C" then gosub random files thingy
  497.  
  498. REM **************************************************************************
  499.  
  500. REM **************************************************************************
  501. REM **** delay loop **********************************************************
  502. REM **************************************************************************
  503. SUB Delay (dlay!)
  504.         start! = TIMER
  505.         DO WHILE start! + dlay! >= TIMER
  506.                 IF start! > TIMER THEN start! = start! - 86400
  507.         LOOP
  508. REM **************************************************************************
  509.  

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: random file puts only one record in file
« Reply #1 on: October 01, 2020, 05:41:54 pm »
Hello

I think i got it.

Badger

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: random file puts only one record in file
« Reply #2 on: October 01, 2020, 08:33:18 pm »
Classic case of not getting enough SLEEP.

Code: QB64: [Select]
  1. IF zcustrecnum = 1 OR zcustrecnum < 2 THEN
  2.  
  3.         PUT #1, zcustrecnum, custinforec
  4.         zcustrecnum = zcustrecnum + 1
  5.         PUT #1, zcustrecnum, custinforec

I'm glad you figured out that code wasn't allowing the zcustrecnum variable to advance past record #1.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: random file puts only one record in file
« Reply #3 on: October 02, 2020, 04:07:51 am »
I think you have hit the centre of the issue

Quote
Classic case of not getting enough SLEEP.

because in this lines of code
Code: QB64: [Select]
  1. IF zcustrecnum = 1 OR zcustrecnum < 2 THEN
  2.  
  3.         PUT #1, zcustrecnum, custinforec
  4.         zcustrecnum = zcustrecnum + 1
  5.         PUT #1, zcustrecnum, custinforec

Quote
PUT #1, zcustrecnum, custinforec
is the same so the code can be srhinked in this manner

Code: QB64: [Select]
  1.  IF zcustrecnum >1 THEN    zcustrecnum = zcustrecnum + 1
  2. PUT #1, zcustrecnum, custinforec
  because a record number cannot be 0 or negative number, so zcustrecnum = 1  is equal to zcustrecnum <2...
and written in this other manner you can argue that as long as zcustrecnum is 1 will never become 2...

Good Shot Pete!
Programming isn't difficult, only it's  consuming time and coffee

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: random file puts only one record in file
« Reply #4 on: October 02, 2020, 02:39:16 pm »
hello

better yet when you open the random file you use
zcustrecnum = lof(1) /lcustreclen.. if there is not records in the file this should be 0.

when the record insertion module or sub is call all you need is

zcustrecnum = zcustrecnum +1
put #1,zcustrecnum, custinforec
it seems to work fine.

Badger