how to "input" into 2d random file?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
hugh
Newbie
Posts: 4
Joined: Sun Dec 07, 2008 5:37 pm

how to "input" into 2d random file?

Post by hugh »

'I have built the 2d array but I can't figure how to get it into a 2d random
'file such as open"c:\jon.data" for random as #1 len=len(rec(x,y))
TYPE records
name AS STRING * 30
job AS STRING * 35
END TYPE
DEFINT A-Z
DIM table(100) AS INTEGER
DIM rec(26, 5) AS records
DIM ray(50) AS records
CLS
FOR o = 1 TO 26
FOR p = 1 TO 3
rec(o, p).name = "0"
rec(o, p).job = "0"
table(o) = 0
NEXT p
NEXT o

INPUT "ENTER HOW MANY RECORDS 0 TO END ", M%
IF M% = 0 THEN END
f = 1

FOR I = 1 TO M%
200 INPUT " enter name ", ray(I).name
ray(I).name = UCASE$(ray(I).name)
INPUT "enter job ", ray(I).job

IF ray(I).name < "A" THEN 200
IF ray(I).name > "[" THEN 200
IF ray(I).name < "B" THEN x = 1
IF x = 1 THEN 300
IF ray(I).name < "C" THEN x = 2
IF x = 2 THEN 300
IF ray(I).name < "D" THEN x = 3
IF x = 3 THEN 300
IF ray(I).name < "E" THEN x = 4
IF x = 4 THEN 300
IF ray(I).name < "F" THEN x = 5
IF x = 5 THEN 300
IF ray(I).name < "G" THEN x = 6
IF x = 6 THEN 300
IF ray(I).name < "H" THEN x = 7
IF x = 7 THEN 300
IF ray(I).name < "I" THEN x = 8
IF x = 8 THEN 300
IF ray(I).name < "J" THEN x = 9
IF x = 9 THEN 300
IF ray(I).name < "K" THEN x = 10
IF x = 10 THEN 300
IF ray(I).name < "L" THEN x = 11
IF x = 11 THEN 300
IF ray(I).name < "M" THEN x = 12
IF x = 12 THEN 300
IF ray(I).name < "N" THEN x = 13
IF x = 13 THEN 300
IF ray(I).name < "O" THEN x = 14
IF x = 14 THEN 300
IF ray(I).name < "P" THEN x = 15
IF x = 15 THEN 300
IF ray(I).name < "Q" THEN x = 16
IF x = 16 THEN 300
IF ray(I).name < "R" THEN x = 17
IF x = 17 THEN 300
IF ray(I).name < "S" THEN x = 18
IF x = 18 THEN 300
IF ray(I).name < "T" THEN x = 19
IF x = 19 THEN 300
IF ray(I).name < "U" THEN x = 20
IF x = 20 THEN 300
IF ray(I).name < "V" THEN x = 21
IF x = 21 THEN 300
IF ray(I).name < "W" THEN x = 22
IF x = 22 THEN 300
IF ray(I).name < "X" THEN x = 23
IF x = 23 THEN 300
IF ray(I).name < "Y" THEN x = 24
IF x = 24 THEN 300
IF ray(I).name < "Z" THEN x = 25
IF x = 25 THEN 300
IF ray(I).name < "a" THEN x = 26
IF x = 26 THEN 300


300 y = 1

IF f = 1 THEN GOTO 510
PRINT x

FOR s = 1 TO 78
p = table(s)
IF p = x THEN y = y + 1

NEXT s
IF y > 1 THEN GOTO 510
y = 1
510 rec(x, y).name = ray(I).name
rec(x, y).job = ray(I).job
f = f + 1
table(I) = x
PRINT x
x = 0
NEXT I
k = 1

FOR c = 1 TO 26

FOR d = 1 TO 3
PRINT rec(c, d).name;
PRINT rec(c, d).job
k = k + 1
IF k = 20 THEN SLEEP
IF k = 40 THEN SLEEP
NEXT d

NEXT c
END
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Why 2D?

Post by burger2227 »

When you create a TYPE, the type variable will hold ALL of the listed variable values inside. From strings to decimal point numbers. So the array can hold a number of records, with all of the type variables in each, without being 2D.

You have to work with the DOT variable names separately to assign all of the data in each record. Just use the Array element number to change to another record.

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
hugh
Newbie
Posts: 4
Joined: Sun Dec 07, 2008 5:37 pm

reply to Ted

Post by hugh »

thanks Ted for your answer

I am learning arrays and two dimensional random files which I thought
when I start all I had to do was like:

type records
name as string * 20
address as string * 20
end type
dim pencil(26,5) as records
open "c:\ rec.data" for random as #1 len= len(pencil(x,y))
then enter data into the array as
pencil(x,y).name
pencil(x,y).address
put#1, x, pencil(x,y) ' the record# is now x
and increment y
enter data the next record with the same x but the next y
pencil(x,y).name ' the y now incremented
pencil(x,y).address
put #1, y, pencil(x,y) ' note record# is now y
but I end up with a mess

this is because I don"t know the preceedure
thanks
Hugh
Post Reply