Inventory Program- help?

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
flirt85
Coder
Posts: 13
Joined: Sun Mar 12, 2006 2:14 pm

Inventory Program- help?

Post by flirt85 »

I'm trying to create a 'simple' program that allows the user to select a specific type of ladder out of data that has different types. In the first "case" or, if the user selects #1 from the menu, the output should read
"Extension Ladders
6 ft aluminum
10 ft aluminum
8 ft aluminum"

#2 output should read
Step Ladders
6 ft fiberglass
10 ft aluminum
6 ft aluminum
8 ft fiberglass
8 ft aluminum

#3 ouput should list the ladders by thier size,
6 FOOT
...
8 FOOT
....
10 FOOT

Currently When #1 or #2 is selected, the entire data list is outputed, and #3 is an endless stream of Fiberglass step.....Any help is appreciated!!!


COLOR 15, 1
CLS

DIM ladder AS STRING 'ladder name
DIM length AS INTEGER 'length of ladder
DIM choice AS INTEGER 'menu choice
DIM x AS INTEGER 'loop variable
DIM sp AS INTEGER 'space

DATA "Fiberglass Step",6
DATA "Aluminum Extension",6
DATA "Aluminum Step",10
DATA "Aluminum Extension",10
DATA "Aluminum Step",6
DATA "Fiberglass Step",8
DATA "Aluminum Extension",8
DATA "Aluminum Step",8
DATA "xxx",-1

CLS
PRINT " Ladders on Hand"
PRINT
PRINT " 1) Extension"
PRINT " 2) Step"
PRINT " 3) List by Size"
PRINT " 4) Exit"
PRINT
PRINT

INPUT " Select a Menu Item"; choice

SELECT CASE choice

CASE 1
CLS
PRINT "Extension Ladders"
READ ladder, length
WHILE ladder <> "xxx"
FOR x = 1 TO LEN(ladder) 'find and assign
IF MID$(ladder, x, 1) = " " THEN sp = x
NEXT x
IF ladder = MID$(ladder, sp + 1, 1) = E THEN
PRINT length; "ft "; LEFT$(ladder, sp - 1)
END IF
READ ladder, length
WEND

CASE 2
CLS
PRINT "Step Ladders"
READ ladder, length
WHILE ladder <> "xxx"
FOR x = 1 TO LEN(ladder) 'find and assign
IF MID$(ladder, x, 1) = " " THEN sp = x
NEXT x
IF ladder = MID$(ladder, sp + 1, 1) = S THEN
PRINT length; "ft "; LEFT$(ladder, sp - 1)
END IF
READ ladder, length
WEND
CASE 3
CLS
PRINT " 6 FOOT"
RESTORE
READ ladder, length
WHILE ladder <> "xxx"
IF length = 6 THEN
PRINT ladder
END IF
WEND
PRINT " 8 FOOT"
RESTORE
READ ladder, length
WHILE ladder <> "xxx"
IF length = 8 THEN
PRINT ladder
END IF
WEND
PRINT " 10 FOOT"
RESTORE
READ ladder, length
WHILE ladder <> "xxx"
IF length = 10 THEN
PRINT ladder
END IF
WEND
CASE 4
END
END SELECT
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Flirt85,
The following code to determine if an extension ladder, won't work.

Code: Select all

      FOR x = 1 TO LEN(ladder)  'find and assign
        IF MID$(ladder, x, 1) = " " THEN sp = x
      NEXT x
      IF ladder = MID$(ladder, sp + 1, 1) = E THEN  '*** THIS IS NO GOOD ***
         PRINT length; "ft "; LEFT$(ladder, sp - 1)
      END IF
Instead, try the following:

Code: Select all

      begin = INSTR(ladder,"Extension")
      IF begin > 0 then
          PRINT length; "ft "; LEFT$(ladder, begin-2)
      END IF 
The same applies to your step ladder logic.

For CASE 3: Just before the WEND for the 6, 8 and 10 foot ladder routines, you're missing the "read ladder,length" statement.

In general, you could simplify your program if you set up 3 data fileds per item, such as:
DATA "Aluiminum" , "Step" , 6
etc.


I don't have DOS available right now to test it, but you try it and let me know.
*****
flirt85
Coder
Posts: 13
Joined: Sun Mar 12, 2006 2:14 pm

data not printed in output

Post by flirt85 »

Good Morning Moneo,

Thank you for your help. I swapped my FOR NEXT code for your BEGIN code, and in case one, the ouput is correct.

However, I did the same swap with the CASE 2 code, and replaced "Extension" with "Step", and I get no output.

Also, with your help, Case 3 is no longer an endless loop. I think I may have another error in my code though, because nothing is printed after "8 Foot", and on.

Please help!
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Flirt85,

It's hard to imagine the modified code of your latest version. Please post the whole thing again.
*****
flirt85
Coder
Posts: 13
Joined: Sun Mar 12, 2006 2:14 pm

Modified Code

Post by flirt85 »

Sorry Moneo, Here it is again:

COLOR 15, 1
CLS

DIM ladder AS STRING 'ladder name
DIM length AS INTEGER 'length of ladder
DIM choice AS INTEGER 'menu choice
DIM x AS INTEGER 'loop variable
DIM sp AS INTEGER 'space

DATA "Fiberglass Step",6
DATA "Aluminum Extension",6
DATA "Aluminum Step",10
DATA "Aluminum Extension",10
DATA "Aluminum Step",6
DATA "Fiberglass Step",8
DATA "Aluminum Extension",8
DATA "Aluminum Step",8
DATA "xxx",-1

CLS
PRINT " Ladders on Hand"
PRINT
PRINT " 1) Extension"
PRINT " 2) Step"
PRINT " 3) List by Size"
PRINT " 4) Exit"
PRINT
PRINT

INPUT " Select a Menu Item"; choice

SELECT CASE choice

CASE 1
CLS
PRINT "Extension Ladders"
READ ladder, length
WHILE ladder <> "xxx"
begin = INSTR(ladder, "Extension")
IF begin > 0 THEN
PRINT length; "ft "; LEFT$(ladder, begin - 2)
END IF
READ ladder, length
WEND

CASE 2
CLS
PRINT "Step Ladders"
READ ladder, length
WHILE ladder <> "xxx"
bring = INSTR(ladder, "Step")
IF begin > 0 THEN
PRINT length; "ft "; LEFT$(ladder, begin - 2)
END IF
READ ladder, length
WEND
CASE 3
CLS
PRINT " 6 FOOT"
RESTORE
READ ladder, length
WHILE ladder <> "xxx"
IF length = 6 THEN
PRINT ladder
END IF
READ ladder, length
WEND
PRINT " 8 FOOT"
RESTORE
READ ladder, length
WHILE ladder <> "xxx"
IF length = 8 THEN
PRINT ladder
END IF
WEND
PRINT " 10 FOOT"
RESTORE
READ ladder, length
WHILE ladder <> "xxx"
IF length = 10 THEN
PRINT ladder
END IF
READ ladder, length
WEND
CASE 4
END
END SELECT




Thank you for all of your help!! I really appreciate it!
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

Flirt85, you will notice below your subject line when you post a message a "code" button. If you push it, copy your code, and push it again, it will format your code to be much more readable. I have taken the liberty of doing this for you.

Code: Select all

COLOR 15, 1
CLS

DIM ladder AS STRING 'ladder name
DIM length AS INTEGER 'length of ladder
DIM choice AS INTEGER 'menu choice
DIM x AS INTEGER 'loop variable
DIM sp AS INTEGER 'space

DATA "Fiberglass Step",6
DATA "Aluminum Extension",6
DATA "Aluminum Step",10
DATA "Aluminum Extension",10
DATA "Aluminum Step",6
DATA "Fiberglass Step",8
DATA "Aluminum Extension",8
DATA "Aluminum Step",8
DATA "xxx",-1

CLS
PRINT " Ladders on Hand"
PRINT
PRINT " 1) Extension"
PRINT " 2) Step"
PRINT " 3) List by Size"
PRINT " 4) Exit"
PRINT
PRINT

INPUT " Select a Menu Item"; choice

SELECT CASE choice

CASE 1
CLS
PRINT "Extension Ladders"
READ ladder, length
WHILE ladder <> "xxx"
begin = INSTR(ladder, "Extension")
IF begin > 0 THEN
PRINT length; "ft "; LEFT$(ladder, begin - 2)
END IF
READ ladder, length
WEND

CASE 2
CLS
PRINT "Step Ladders"
READ ladder, length
WHILE ladder <> "xxx"
bring = INSTR(ladder, "Step")
IF begin > 0 THEN
PRINT length; "ft "; LEFT$(ladder, begin - 2)
END IF
READ ladder, length
WEND
CASE 3
CLS
PRINT " 6 FOOT"
RESTORE
READ ladder, length
WHILE ladder <> "xxx"
IF length = 6 THEN
PRINT ladder
END IF
READ ladder, length
WEND
PRINT " 8 FOOT"
RESTORE
READ ladder, length
WHILE ladder <> "xxx"
IF length = 8 THEN
PRINT ladder
END IF
WEND
PRINT " 10 FOOT"
RESTORE
READ ladder, length
WHILE ladder <> "xxx"
IF length = 10 THEN
PRINT ladder
END IF
READ ladder, length
WEND
CASE 4
END
END SELECT 
Image
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Nathan1993 wrote:Flirt85, you will notice below your subject line when you post a message a "code" button. If you push it, copy your code, and push it again, it will format your code to be much more readable. I have taken the liberty of doing this for you.
Flirt85,
Nathan1993 was trying to help out. Unfortunately your code is not indented, so using the "code" button is not going to help.

In my opinion, you should get in the habit of indenting your code to make it more readable. An example:
CASE 1
CLS
PRINT "Extension Ladders"
READ ladder, length
WHILE ladder <> "xxx"
begin = INSTR(ladder, "Extension")
IF begin > 0 THEN
PRINT length; "ft "; LEFT$(ladder, begin - 2)
END IF
READ ladder, length
WEND
*****************************************
The above would be more readable as:

Code: Select all

CASE 1
  CLS
  PRINT "Extension Ladders"
  READ ladder, length
  WHILE ladder <> "xxx"
              begin = INSTR(ladder, "Extension")
              IF begin > 0 THEN
                  PRINT length; "ft "; LEFT$(ladder, begin - 2)
              END IF
              READ ladder, length
  WEND
What the "code" option does is preserve your indentation. The number of spaces that you indent is entirely up to you, just as long as things line up, like the IF which lines up with the its terminating END IF.

Try it next time.
*****
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Flirt85,

The problem with CASE 2 is that you used a variable named BRING instead of BEGIN.

bring = INSTR(ladder, "Step")

In CASE 3, at the end of you FOOT 8 logic you're missing the READ statement, as so:
END IF
READ ladder, length '***** Missing for FOOT 8.
WEND

The code for FOOT 6, 8 and 10 are so similar that I would have made a subroutine (maybe you prefer a SUB) to avoid redundant code, and also to avoid having one part work and the other not. Example:

CASE 3
CLS
WHICHFT=6
GOSUB FOOTLOGIC
WHICHFT=8
GOSUB FOOTLOGIC
WHICHFT=10
GOSUB FOOTLOGIC
CASE 4
etc....

FOOTLOGIC:
PRINT WHICHFT;" FOOT"
RESTORE
READ ladder, length
WHILE ladder <> "xxx"
IF length = WHICHFT THEN
PRINT ladder
END IF
READ ladder, length
WEND
RETURN '>>>>>>>>>>>>>>

My sample code is intended to be simple. You can make it fancier by:
CASE 3
CLS
FOR WHICHFT=6 TO 10 STEP 2
GOSUB FOOTLOGIC
NEXT WHICHFT

In which case you could just take the code of the subroutine and stick it into the FOR loop replacing the GOSUB.

The trouble with this type of fancier code is that the day that you have to work with a 7 foot ladder, for example, the fancy code doesn't work anymore, and you have to go back to a simple subrouting approach or come up with a clever way to do a FOR LOOP on values of 6, 7, 8 and 10. Is it worth it?
*****
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

Moneo, I was talking about it being more readable on the forums, not it QB. It's easier on the eyes, you know? Not using code tags is one of my pet peeves.
Image
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Nathan1993 wrote:Moneo, I was talking about it being more readable on the forums, not it QB. It's easier on the eyes, you know? Not using code tags is one of my pet peeves.
In order for code to be more readable on the forums, it has to already be indented. If it's not indented, then enclosing the code in a colored box is not going to make it any more readable. Do you agree?
*****
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

I do not agree. A colored box, actually, does make it easier on my eyes to raed the code, whether or not it is indented... I didn't want to start a fight, it's just a good habit to get into. It also helps me know when it's code...
Image
Post Reply