Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me debug my n00b program

Status
Not open for further replies.

TJandE

Programmer
Nov 3, 2002
2
CA
I'm currently working on this to try an easy program, but I'm having troubles. I'm still relatively new to qbasic, and I was wondering if anyone could help me debug this

DECLARE SUB edit ()
DECLARE SUB delete ()
DECLARE SUB putrecord ()
DECLARE SUB getrecord ()
DECLARE SUB showall ()
DECLARE SUB setscreen ()
COMMON SHARED nmbrrcrds%
COMMON SHARED FLAG
REM **Put or Get Records, invent.cat File**

SCREEN 12
CLS
REM **Define structure of invent.cat file**
TYPE filestructure
item AS STRING * 20
price AS SINGLE
quantity AS SINGLE
END TYPE

REM **Declare a record variable of above type**
DIM invent AS filestructure

REM **Open the file**
OPEN "invent.cat" FOR RANDOM AS #1 LEN = LEN(invent)

REM **Put or Get records, or quit (ESC)**
DO
'find out what to do
CLS : setscreen: LOCATE 24, 7: PRINT "put or get individual records, invent.cat file."
nmbrrcrds% = LOF(1) \ LEN(invent)
LOCATE 25, 7: PRINT "invent.cat has"; nmbrrcrds%; "records"
LOCATE 26, 7: PRINT "(D)elete, (E)dit, (G)et, (P)ut, (S)how all, (ESC)ape";
LOCATE 27, 7: PRINT "Selected command:"

DO
kbd$ = UCASE$(INPUT$(1))
LOOP UNTIL kbd$ = "P" OR kbd$ = "G" OR kbd$ = CHR$(27) OR kbd$ = "S" OR kbd$ = "D" OR kbd$ = "E"

'Perform requested operation, PUT (P) or GET (G) or ESC
SELECT CASE PorGorSorEsc$
CASE CHR$(27)
EXIT DO
CASE "P"
CALL putrecord
CASE "G"
CALL getrecord
CASE "S"
CALL showall
CASE "D"
CALL delete
CASE "E"
CALL edit
END SELECT
LOOP

REM **End of program stuff**
CLOSE #1
LOCATE 28, 35: : PRINT "The invent.cat file is closed"
END

SUB delete
REM **Declare invent as record defined in main program**
DIM invent AS filestructure
'DIM invent2 AS newfile

REM **Assign correct record number and check for errors**
LOCATE 28, 20: INPUT "Record number to delete?"; rcrdnmbr
IF rcrdnmbr < 1 THEN
LOCATE 15, 35: PRINT &quot;Sorry, but you must pick:&quot;
LOCATE 16, 25: PRINT &quot;a record number that is within&quot;
LOCATE 17, 25: PRINT &quot;the range.&quot;
LOCATE 18, 25: PRINT &quot;Press a ket to continue&quot;, continue$
EXIT SUB
ELSE
rcrdnmbr = rcrnmbr - 1
END IF

REM **Open a temporary file**
OPEN &quot;invent2.tmp&quot; FOR RANDOM AS #2 LEN = LEN(invent2)

REM **PUt record from 1 up to record to delete into temp file 2**
FOR rcrdnmbr = 1 TO rcrdnmbr
GET #1, rcrdnmbr, invent
invent2.item = invent.item
invent2.price = invent.price
invent2.quantity = invent.quantity
PUT #2, rdcrdnmbr, invent2
NEXT rcrdnmbr

REM **End of one**

REM **Put records from from record to delete to eof into temp file 2
IF rcrdnmbr > nmbrrcrds% THEN
LOCATE 15, 35: PRINT &quot;Sorry, but you must pick&quot;
LOCATE 16, 35: PRINT &quot;a record number that's within&quot;
LOCATE 17, 35: PRINT &quot;the range.&quot;
LOCATE 18, 35: PRINT &quot;Press a key to continue&quot;, continue$
EXIT SUB
ELSE
rdrdnmbr = rcrdnmbr + 1
END IF

FOR rcrdnmbr = rcrdnmbr TO nmbrrcrds%
GET #1, rcrdnmbr, invent
invent2.item = invent.item
invent2.price = invent.price
invent2.quantity = invent.quantity
PUT #2, rcrdnmbr - 1, invent2
NEXT rcrdnmbr

REM **Close off files**
CLOSE

REM **Delete original record**
KILL &quot;invent.cat&quot;

REM **Rename temp files as new file
NAME &quot;invent2.tmp&quot; AS &quot;invent.cat&quot;

LOCATE 18, 35: PRINT &quot;Press a key to continue&quot;
kbd$ = INPUT$(1)

REM **Reopen original file**
OPEN &quot;Invent.cat&quot; FOR RANDOM AS #1 LEN = LEN(invent)

END SUB

SUB edit
'Declare invent as record define in main program
DIM invent AS filestructure
LOCATE 28, 20: INPUT &quot;Record number&quot;; rcrdnmbr

GET #1, rcrdnmbr, invent

REM **Change item name**
LOCATE 12, 20: : PRINT &quot;Change Item Name?&quot;; invent.item
LOCATE 12, 50: : INPUT &quot;(Y)es/(N)o &quot;, answ$
IF UCASE$(answ$) = &quot;Y&quot; THEN
LOCATE 13, 20: LINE INPUT &quot;Name of Item?&quot;; invent.item

REM **Change item price**
LOCATE 14, 20: : PRINT &quot;Change Item Price?&quot;; invent.price
LOCATE 14, 50: : INPUT &quot;(Y)es/(N)o &quot;, answ$
IF UCASE$(answ$) = &quot;Y&quot; THEN
LOCATE 15, 20: LINE INPUT &quot;New Price?&quot;; invent.price

REM **Change item name**
LOCATE 16, 20: : PRINT &quot;Change Item Quantity?&quot;; invent.quantity
LOCATE 16, 50: : INPUT &quot;(Y)es/(N)o &quot;, answ$
IF UCASE$(answ$) = &quot;Y&quot; THEN
LOCATE 17, 20: LINE INPUT &quot;New Quantity?&quot;; invent.quantity
END IF
PUT #1, rcrdnmbr, invent
LOCATE 20, 35: : PRINT &quot;Press a key to continue&quot;
kbd$ = INPUT$(1)

END SUB

SUB getrecord
'Declare Invent as record defined in main program
DIM invent AS filestructure
LOCATE 28, 20: INPUT &quot;record number&quot;; rcrdnmbr
GET #1, rdrdnmbr, invent
LOCATE 12, 20: PRINT &quot;Name of Item&quot;; invent.item
LOCATE 14, 20: PRINT &quot;Price of Item;&quot;; USING &quot;$$###,###,###,###&quot;; invent.price
LOCATE 16, 20: PRINT &quot;Quantity left&quot;; invent.quantity
LOCATE 18.35: PRINT &quot;Press a key to continue&quot;
kbd$ = INPUT$(1)
END SUB

SUB putrecord
'Declare Invent as record defined in main program
DIM invent AS filestructure
LOCATE 28, 20: INPUT &quot;record number&quot;; rcrdnmbr
LOCATE 12, 20: PRINT &quot;Name of Item?&quot;; invent.item
LOCATE 14, 20: PRINT &quot;Price of Item?&quot;; invent.price
LOCATE 16, 20: PRINT &quot;Quantity left&quot;; invent.quantity
PUT #1, rcrdnmbr, invent
LOCATE 18.35: PRINT &quot;Press a key to continue&quot;
kbd$ = INPUT$(1)

END SUB

SUB setscreen
REM **Large screen box**
LINE (35, 10)-(569, 335), 8, BF'gray
LINE (25, 5)-(550, 325), 1, BF'blue

REM **Title box**
LINE (50, 25)-(484, 100), 8, BF'gray
LINE (55, 30)-(480, 90), 11, BF'tope??
LOCATE 4, 25: PRINT &quot; Dustin's Fantaboular:&quot;
LOCATE 4, 20: PRINT &quot;Inventory Control Program&quot;
REM **Stat box**

REM **menu box**
LINE (35, 255)-(560, 465), 8, BF'gray
LINE (25, 350)-(550, 455), 1, BF'blue

END SUB

SUB showall
DIM invent AS filestructure
format1$ = &quot;$$###,###,###&quot;
format2$ = &quot;,#######,&quot;
row = 10
FOR rcrdnmbr = 1 TO nmbrrcrds%
GET #1, rcrdnmbr, invent
LOCATE 9, 15: PRINT &quot;Num#Item Item Price Quantities Remaining&quot;
row = row + 1
LOCATE row, 15: PRINT rcrdnmbr; &quot;.&quot;
LOCATE row, 20: PRINT invent.item;
LOCATE row, 28: PRINT USING &quot;$$###,###,###.##&quot;; invent.price
LOCATE row, 43: PRINT USING format2$; invent.quantity
LOCATE 18, 55: PRINT &quot;Press a key&quot;: LOCATE 19, 55: PRINT &quot;to continue.&quot;
sortec = sortec + 1
IF sortec = 10 THEN
sortec = 1
row = 10
LOCATE 18, 55: PRINT &quot;Press a key&quot;: LOCATE 19, 55: INPUT &quot;for more. &quot;, more
REM **Large screen box**
LINE (35, 10)-(550, 335), 8, BF'gray
LINE (25, 5)-(550, 325), 1, BF'tope

REM **Title Box**
LINE (50, 25)-(485, 100), 8, BF'gray
LINE (55, 30)-(480, 90), 11, BF'tope??
LOCATE 4, 25: PRINT &quot;Dustin's Fantaboulus&quot;
LOCATE 5, 20: PRINT &quot;Inventory Control Program&quot;
END IF
NEXT rcrdnmbr
kbd$ = INPUT$(1)

END SUB

Like I said i'm new and i'm not quite sure what I have to change.
 
Whats going wrong with it? What type of error are your getting?
 
Hmm...below is a quick re-write after I've looked over it; most of my changes are highlighted in red. (not necessarily better...not necessarily that everything I changed was wrong...just re-wrote it how I'm most comfortable writing the prog.)
Hope this helps you ;-)

DECLARE SUB edit ()
DECLARE SUB delete ()
DECLARE SUB putrecord ()
DECLARE SUB getrecord ()
DECLARE SUB showall ()
DECLARE SUB setscreen ()
COMMON SHARED nmbrrcrds%
COMMON SHARED FLAG
REM **Put or Get Records, invent.cat File**

SCREEN 12
CLS
REM **Define structure of invent.cat file**
TYPE filestructure
item AS STRING * 20
price AS STRING * 16 'Enter formatted prices i.e. $xxx,xxx,xxx,xxx
quantity AS STRING * 4 'Allows for up to 4-digit quantities to be entered/stored
END TYPE

REM **Declare shared variables**
DIM SHARED invent AS filestructure
DIM SHARED invent2 AS filestructure
DIM SHARED rcrdnmbr AS INTEGER


REM **Open the file**
OPEN &quot;invent.cat&quot; FOR RANDOM AS #1 LEN = LEN(invent)

REM **Put or Get records, or quit (ESC)**
DO
'find out what to do
CLS : setscreen: LOCATE 24, 7: PRINT &quot;put or get individual records, invent.cat file.&quot;
nmbrrcrds% = LOF(1) \ LEN(invent)
LOCATE 25, 7: PRINT &quot;invent.cat has&quot;; nmbrrcrds%; &quot;records&quot;
LOCATE 26, 7: PRINT &quot;(D)elete, (E)dit, (G)et, (P)ut, (S)how all, (ESC)ape&quot;;
INKEY$ = &quot;&quot;
DO
kbd$ = UCASE$(INKEY$)
LOOP UNTIL kbd$ = &quot;P&quot; OR kbd$ = &quot;G&quot; OR kbd$ = CHR$(27)
OR kbd$ = &quot;S&quot; OR kbd$ = &quot;D&quot; OR kbd$ = &quot;E&quot;
CLS

'Perform requested operation, PUT (P) or GET (G) or ESC
IF kbd$ == &quot;P&quot;
CALL putrecord
ELSEIF kbd$ == &quot;G&quot; THEN
CALL getrecord
ELSEIF kbd$ == &quot;S&quot; THEN
CALL showall
ELSEIF kbd$ == &quot;D&quot; THEN
CALL delete
ELSEIF kbd$ == &quot;E&quot; THEN
CALL edit
ELSEIF kbd$ == CHR$(27) THEN
EXIT DO
END IF

LOOP

REM **End of program stuff**
CLOSE #1
LOCATE 28, 35: : PRINT &quot;The invent.cat file is closed&quot;
END

SUB delete
REM **Declare invent as record defined in main program(already DIMed)**

REM **Assign correct record number and check for errors**
DO
LOCATE 28, 20: INPUT &quot;Record number to delete?&quot;; rcrdnmbr
IF rcrdnmbr > 0 THEN
IF rcrdnmbr <= nmbrrcrds%
rcrdnmbr = rcrnmbr - 1
EXIT DO
END IF
ELSE
CLS
LOCATE 14, 25: PRINT &quot;Sorry, but you must pick&quot;
LOCATE 15, 25: PRINT &quot;a record number that is within&quot;
LOCATE 16, 25: PRINT &quot;the range.&quot;
LOCATE 18, 25: PRINT &quot;Press [ESC] to return to main menu&quot;
LOCATE 19, 25: PRINT &quot;Any other key to try again&quot;
INKEY$ = &quot;&quot;
DO
IF INKEY$ <> &quot;&quot; THEN
IF INKEY$ == CHR$(27) THEN
EXIT DO
EXIT DO
END SUB
END IF
EXIT DO
LOOP
END IF
LOOP
END IF

REM **Open a temporary file**
OPEN &quot;invent2.tmp&quot; FOR RANDOM AS #2 LEN = LEN(invent)

REM **PUT recordS from 1 up to record to delete into temp file 2**
FOR tempnmbr! = 1 TO rcrdnmbr
GET #1, tempnmbr!, invent
invent2.item = invent.item
invent2.price = invent.price
invent2.quantity = invent.quantity
PUT #2, tempnmbr!, invent2
NEXT tempnmbr!

REM **End of one**

REM **Put records from from record to delete to eof into temp file 2**
rcdrdnmbr = rcrdnmbr + 2

FOR tempnmbr! = rcrdnmbr TO nmbrrcrds%
GET #1, tempnmbr!, invent
invent2.item = invent.item
invent2.price = invent.price
invent2.quantity = invent.quantity
PUT #2, tempnmbr! - 1, invent2
NEXT tempnmbr!


REM **Close off files**
CLOSE

REM **Delete original record**
KILL &quot;invent.cat&quot;

REM **Rename temp file as permanent data file**
NAME &quot;invent2.tmp&quot; AS &quot;invent.cat&quot;

CLS
LOCATE 18, 35: PRINT &quot;Press any key to return to main menu&quot;
INKEY$ = &quot;&quot;
DO
IF INKEY$ <> &quot;&quot; THEN
EXIT DO
END IF
LOOP
CLS

REM **Reopen original file**
OPEN &quot;Invent.cat&quot; FOR RANDOM AS #1 LEN = LEN(invent)

END SUB


SUB edit
'Declare invent as record defined in main program (already DIMed)
CLS
LOCATE 28, 20: INPUT &quot;Record number:&quot;; rcrdnmbr
CLS

GET #1, rcrdnmbr, invent

REM **Change item name**
CLS
LOCATE 12, 20: : PRINT &quot;Change Item Name?&quot;; invent.item
LOCATE 12, 50: : PRINT &quot;(Y)es/(N)o&quot;
INKEY$ = &quot;&quot;
DO
answ$ = UCASE$(INKEY$)
LOOP UNTIL answ$ = &quot;Y&quot; OR answ$=&quot;N&quot;
CLS
IF answ$ = &quot;Y&quot; THEN

CLS
LOCATE 13, 20: INPUT &quot;Name of Item:&quot;, invent.item
CLS
END IF

REM **Change item price**
LOCATE 14, 20: : PRINT &quot;Change Item Price?&quot;; invent.price
CLS
LOCATE 12, 50: : PRINT &quot;(Y)es/(N)o&quot;
INKEY$ = &quot;&quot;
DO
answ$ = UCASE$(INKEY$)
LOOP UNTIL answ$ = &quot;Y&quot; OR answ$=&quot;N&quot;
CLS
IF answ$ = &quot;Y&quot; THEN

CLS
LOCATE 15, 20: INPUT &quot;New Price?&quot;; invent.price
CLS
END IF

REM **Change item name**
CLS
LOCATE 16, 20: : PRINT &quot;Change Item Quantity?&quot;; invent.quantity
LOCATE 12, 50: : PRINT &quot;(Y)es/(N)o&quot;
INKEY$ = &quot;&quot;
DO
answ$ = UCASE$(INKEY$)
LOOP UNTIL answ$ = &quot;Y&quot; OR answ$=&quot;N&quot;
CLS
IF answ$ = &quot;Y&quot; THEN

CLS
LOCATE 17, 20: INPUT &quot;New Quantity?&quot;; invent.quantity
CLS
END IF

PUT #1, rcrdnmbr, invent

CLS
LOCATE 20, 35: PRINT &quot;Press any key to return to main menu&quot;
INKEY$ = &quot;&quot;
DO
IF INKEY$ <> &quot;&quot; THEN
EXIT DO
END IF
LOOP
CLS

END SUB

SUB getrecord
CLS
LOCATE 28, 20: INPUT &quot;record number&quot;; rcrdnmbr
CLS
GET #1, rdrdnmbr, invent
CLS
LOCATE 12, 20: PRINT &quot;Name of Item:&quot;; invent.item
LOCATE 14, 20: PRINT &quot;Price of Item:&quot;; invent.price
LOCATE 16, 20: PRINT &quot;Quantity Left&quot;; invent.quantity
LOCATE 18, 35: PRINT &quot;Press any key to return to main menu&quot;
DO
IF INKEY$ <> &quot;&quot; THEN
EXIT DO
END IF
LOOP
CLS
END SUB

SUB putrecord
CLS
rcrdnmbr = nmbrrcrds% + 1 'Appends new record to end of file...avoids overlapping records
LOCATE 12, 20: PRINT &quot;Name of Item?&quot;; invent.item
LOCATE 14, 20: PRINT &quot;Price of Item?&quot;; invent.price
LOCATE 16, 20: PRINT &quot;Quantity left?&quot;; invent.quantity
CLS
PUT #1, rcrdnmbr, invent
LOCATE 18, 35: PRINT &quot;Press any key to return to main menu&quot;
DO
IF INKEY$ <> &quot;&quot; THEN
EXIT DO
END IF
LOOP
CLS

END SUB

SUB setscreen
REM **Large screen box**
LINE (35, 10)-(569, 335), 8, BF'gray
LINE (25, 5)-(550, 325), 1, BF'blue

REM **Title box**
LINE (50, 25)-(484, 100), 8, BF'gray
LINE (55, 30)-(480, 90), 11, BF'tope??
LOCATE 4, 25: PRINT &quot; Dustin's Fantaboular:&quot;
LOCATE 4, 20: PRINT &quot;Inventory Control Program&quot;
REM **Stat box**

REM **menu box**
LINE (35, 255)-(560, 465), 8, BF'gray
LINE (25, 350)-(550, 455), 1, BF'blue

END SUB

SUB showall

rcrdnmbr = 1
DO
CLS
REM **Large screen box**
LINE (35, 10)-(550, 335), 8, BF'gray
LINE (25, 5)-(550, 325), 1, BF'tope

REM **Title Box**
LINE (50, 25)-(485, 100), 8, BF'gray
LINE (55, 30)-(480, 90), 11, BF'tope??

LOCATE 4, 25: PRINT &quot;Dustin's Fantaboulus&quot;
LOCATE 5, 20: PRINT &quot;Inventory Control Program&quot;
LOCATE 9, 15: PRINT &quot;# Item Item Price Quantities Remaining&quot;
FOR row! = 10 to 19
rcrdnmbr = rcrdnmbr + 1
IF rcrdnmbr > nmbrrcrds% THEN
EXIT FOR
END IF
GET #1, rcrdnmbr, invent
LOCATE row!, 15: PRINT rcrdnmbr; &quot;.&quot;
LOCATE row!, 18: PRINT invent.item
LOCATE row!, 29: PRINT invent.price
LOCATE row!, 44: PRINT invent.quantity
NEXT row!
LOCATE 21, 35: PRINT &quot;Press any key for more.&quot;
DO
IF INKEY$ <> &quot;&quot; THEN
EXIT DO
END IF
LOOP
CLS
LOOP UNTIL rcrdnmbr >= nmbrrcrds%
LOCATE 18, 35: PRINT &quot;Press any key to return to main menu&quot;
DO
IF INKEY$ <> &quot;&quot; THEN
EXIT DO
END IF
LOOP
CLS

END SUB -Robherc
robherc@hotmail.com
(Anybody remember me? ;-)
 
Awesome, Thanks a lot, this really helps!!
 
Just to point out, though, that the original design was better in terms of structure and modularity. It made better use of language features and was preferable in many ways except that according to its author it did not work :) For instance, robherc removed the [tt]SELECT CASE[/tt] and replaced it with a series of [tt]IF/ELSEIF[/tt] statements.

TJandE: You should look into passing parameters to [tt]SUB[/tt]s, your design of using global variables, while functional, is considered deprecated (out-of-date, replaced by superior functionality). Also note that when you do need to use global variables, it suffices to use [tt]DIM SHARED[/tt] to declare them; [tt]COMMON[/tt] and [tt]COMMON SHARED[/tt] are for crossing module boundaries (which means using multiple .BAS source code files in one program).

As for debugging, it still isn't clear what your original error was, but if you haven't already, you should investigate the IDE's debugging capabilities -- stepping through the source code, watching the value of expressions, breakpoints, etc. The F8 key will step into the next instruction, F10 will step over the next instruction (if it calls a [tt]SUB[/tt], the debugger won't go back into 'break' mode until after the subroutine has finished executing). The essence of the debugger is that you can pause the program to verify that it is in the correct state, and then unpause it for a controlled period of computation, be it one statement or a function call or whatever, after which it pauses again (&quot;breaks&quot;). You can enter debug mode at any time when running a program within QuickBASIC by pressing Ctrl+Break (the Break key is the same as the Pause key).

Finally, it is not considered completely outrageous to write other small programs to read in and test data files, and to run them while the main program is in break mode to verify that everything is going according to plan :) In fact, many programmers depend on this debugging technique.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top