• Print

Author Topic: "Newby" ... there is a flaw in my code... any ideas?  (Read 532 times)

mballa01

  • Guest
"Newby" ... there is a flaw in my code... any ideas?
« on: November 29, 2010, 07:31:37 PM »
My code is not validating the item number in the list... I am new to QB so I am having some trouble with getting it to validate from the list.

Code: [Select]
DIM name AS STRING
DIM address AS STRING
DIM item AS INTEGER
DIM quantity AS INTEGER
DIM price AS INTEGER
DIM sub AS INTEGER
DIM foundIt AS STRING
DIM MSG_YES AS STRING
DIM MSG_NO AS STRING
size = 6
DIM Valid_Item(1 TO size) AS INTEGER
DIM Valid_Item_Price(1 TO size)
Valid_Item(1) = 106
Valid_Item(2) = 108
Valid_Item(3) = 307
Valid_Item(4) = 405
Valid_Item(5) = 457
Valid_Item(6) = 688
Valid_Item_Price(1) = 0.59
Valid_Item_Price(2) = 0.99
Valid_Item_Price(3) = 4.50
Valid_Item_Price(4) = 15.99
Valid_Item_Price(5) = 17.50
Valid_Item_Price(6) = 39.00
foundIt = "N"
MSG_YES = "Item available"
MSG_NO = "Item not found"
PRINT "Please enter your name:"
INPUT name
PRINT "Please enter your address:"
INPUT address
PRINT "Please enter your item number:"
INPUT item
PRINT "Please enter your quantity:"
INPUT quantity
LET sub = 0
WHILE sub < size
    IF item = Valid_Item(sub) THEN
        foundIt = "Y"
        price = Valid_Item_Price(sub)
    END IF
    LET sub = sub + 1
WEND
IF foundIt = "Y" THEN
    PRINT MSG_YES
    PRINT quantity, " at", price, " each"
    PRINT "Total ", quantity * price
ELSE
    PRINT MSG_NO
END IF
« Last Edit: November 29, 2010, 08:29:37 PM by mballa01 »

unseenmachine

  • Hero Member
  • *****
  • Posts: 3285
  • A fish, a fish, a fishy o!
Re: "Newby" ... there is a flaw in my code... any ideas?
« Reply #1 on: November 29, 2010, 07:50:12 PM »
Valid_Item and valid_item_price need to be arrays,not size. also LET is a legacy statement your code will run perfectly without it

mystring$ = "Hello"

is the same as

LET mystring$ = "Hello"

Oh, and welcome to the forum.
UnseenGDK Download : http://dl.dropbox.com/u/8822351/UnseenGDK.bm
GDK Tutorial : http://dl.dropbox.com/u/8822351/UnseenGDK_Tutorial.doc
VQB02 : http://dl.dropbox.com/u/8822351/VQB02.zip

DarthWho

  • Hero Member
  • *****
  • Posts: 3853
  • Timelord of the Sith
Re: "Newby" ... there is a flaw in my code... any ideas?
« Reply #2 on: November 29, 2010, 08:10:15 PM »
welcome to the forum
unseen is largely correct but for those two arrays (valid_item and valid_item_price) I would suggest SINGLE for valid_item_price and valid_item as INTEGER IE
Code: [Select]
DIM Valid_item(1 TO 6) AS INTEGER
DIM Valid_item_PRICE(1 TO 6) AS SINGLE
lot of cool code about the QB64 forum 3d engines graphics
if you are looking to program for graphics the UNSEEN GFX/GDK library has a lot of cool functions
BTW to do the code window thing just highlight your code and hit the button that has # when posting
sorry about this but I am spreading the word about it
***********ADVERTISEMENT************
DKM SLOGAN CONTEST got to the Digital Knife Monkey Productions!!! Topic to submit your suggestion
***********END ADVERTISEMENT*********
Rassilon: My lord Doctor; My lord Master; My lord DarthWho
The Doctor and the master at the same time :WHAT!?!?!

FastMath 1.1.0 released: http://dl.dropbox.com/u/12359848/fastmath.h

Pete

  • Moderator
  • Hero Member
  • *****
  • Posts: 6240
  • Cuz I sez so varmint!
Re: "Newby" ... there is a flaw in my code... any ideas?
« Reply #3 on: November 29, 2010, 08:33:49 PM »

Ah... DKM, You'll go ape _hit for our code.

DKM, Just a few chromosomes short of perfection.

DKM, come join the wild life.

DKM, because everyone else is just monkeying around.

DKM, our business ain't monkey business.

DKM, you'll love what we're flinging!

DKM, hear no FB, see no FB, code no FB.

DKM, we are a-peeling.

--------------------------

Pete  ;D

It's only rocket science; it's not Linux!

DarthWho

  • Hero Member
  • *****
  • Posts: 3853
  • Timelord of the Sith
Re: "Newby" ... there is a flaw in my code... any ideas?
« Reply #4 on: November 29, 2010, 08:39:14 PM »
I said go to the DKM topic but I'll copy it over though those are jokes too right I posted a few joke slogans of my own
Rassilon: My lord Doctor; My lord Master; My lord DarthWho
The Doctor and the master at the same time :WHAT!?!?!

FastMath 1.1.0 released: http://dl.dropbox.com/u/12359848/fastmath.h

codeguy

  • Hero Member
  • *****
  • Posts: 3552
  • what the h3ll did i name that code?
    • stuff at dkm
    • Email
Re: "Newby" ... there is a flaw in my code... any ideas?
« Reply #5 on: November 29, 2010, 08:48:06 PM »
first, i wanna break ya of some bad habits:
Code: [Select]
'* why this record? it is far easier to change record contents than add variables in a program
TYPE Merchandise
    Cost AS LONG
    InvNum AS LONG
END TYPE

DIM foundIt AS STRING
DIM MSG_YES AS STRING
DIM MSG_NO AS STRING
MSG_YES = "Item available"
MSG_NO = "Item not found"
REDIM Items(1 TO 6) AS Merchandise '* redim so if ya have increasing amounts of stuff then ya can expand this array accordingly
READ StopValue& '* this is a sentinel value, and how i make a program stop reading a certain set of data
size& = LBOUND(items) 'use longs as they are relatively quick to work with numerically and have a nice wide range
DO
    READ x&
    IF x& = StopValue& THEN
        EXIT DO
    ELSE
        IF size& > UBOUND(items) THEN
            REDIM _PRESERVE Items(size& + 32) AS Merchandise
        END IF
        Items(size&).InvNum = x&
        READ Items(size&).Cost
        size& = size& + 1
    END IF
LOOP
'* data and read are easier on the eyes than a bunch of let statememts
DATA -1,106,108,307,405,457,688
DATA 59,99,450,1599,1750,3900,-1

foundIt = "N"
INPUT "Enter your name:"; name
INPUT "Enter your address:"; address
INPUT "Enter your item number:"; Item&
INPUT "Enter your quantity:"; quantity&
subx& = LBOUND(Items) '* for future reference, SUB is a qb64 reserved word and should NOT be used as a variable
WHILE subx& < size&
    IF Item& = Items(subx&).InvNum THEN
        PRINT MSG_YES
        PRINT quantity&, " at", Items(subx&).Cost / 100, " each"
        TCost# = (quantity& * Items(subx&).Cost)
        Cents% = TCost# MOD 100
        Dollars& = (TCost# - Cents%) \ 100
        PRINT USING "Total $##,### 0#cents"; Dollars&, Cents% '* this prints a leading zero on cents if it is less than 10
        EXIT WHILE
    ELSE
        IF subx& < size& THEN
            subx& = subx& + 1
        ELSE
            PRINT MSG_NO
            EXIT WHILE
        END IF
    END IF
WEND
the way ya had your code structured, if ya had millions of items, it would've cycled through all af them before terminating the while loop -- ouch! that's wasted cpu cycles. mine also only increments if the item is not found and has not reached the end of the list. i wanna welcome ya to qb64. hopefully while you're here, you'll learn some cool things and maybe one day contribute some algorithmic gem yourself. if ya follow the advice of some of our most talented programmers, you'll be a qb64 master in no time flat! ;D
« Last Edit: November 29, 2010, 09:50:32 PM by codeguy »
http://denteddisk.forums-free.com/make-an-appointment-with-the-resident-code-guru-f34.html

Pete

  • Moderator
  • Hero Member
  • *****
  • Posts: 6240
  • Cuz I sez so varmint!
Re: "Newby" ... there is a flaw in my code... any ideas?
« Reply #6 on: November 29, 2010, 09:07:53 PM »

The trouble is, when you change a beginners code that much, he often gets lost. A small percent of the time, such a change results in grand enlightenment, a spiritual awakening, and a brief departure from irregularity.

Pete  ;D
It's only rocket science; it's not Linux!

DarthWho

  • Hero Member
  • *****
  • Posts: 3853
  • Timelord of the Sith
Re: "Newby" ... there is a flaw in my code... any ideas?
« Reply #7 on: November 29, 2010, 09:10:11 PM »
But wouldn't that be irregular in itself???
Why there must be a half dozen ways that programmers on this site draw circles heck i know i have my own method
Rassilon: My lord Doctor; My lord Master; My lord DarthWho
The Doctor and the master at the same time :WHAT!?!?!

FastMath 1.1.0 released: http://dl.dropbox.com/u/12359848/fastmath.h

Pete

  • Moderator
  • Hero Member
  • *****
  • Posts: 6240
  • Cuz I sez so varmint!
Re: "Newby" ... there is a flaw in my code... any ideas?
« Reply #8 on: November 29, 2010, 09:14:32 PM »

My method of drawing circles could draw circles around your method of drawing circles!

Pete  ;D
It's only rocket science; it's not Linux!

codeguy

  • Hero Member
  • *****
  • Posts: 3552
  • what the h3ll did i name that code?
    • stuff at dkm
    • Email
Re: "Newby" ... there is a flaw in my code... any ideas?
« Reply #9 on: November 29, 2010, 09:16:48 PM »
as i stated my main issues with his code were:
LET statements in place of iterative reading of data into a record type
a while statement that terminated only after searching through the entire list even after something is found
not labeling variable types that weren't declared strings
textbook structure (not always a good thing)
and the ability to see code how a good coder would do it. (i think i'm a good coder).
less time writing code = more time for pizza & mountain dew! ;D
http://denteddisk.forums-free.com/make-an-appointment-with-the-resident-code-guru-f34.html

mballa01

  • Guest
Re: "Newby" ... there is a flaw in my code... any ideas?
« Reply #10 on: November 29, 2010, 09:19:14 PM »
codeguy - thanks for the info.  Great coding!  This was an assignment I was tasked with.  So I have to keep it simple, but your code is easy for me to follow and does make more sense.  I'm looking forward to learning more.  This site has been a great learning experience for me.

DarthWho

  • Hero Member
  • *****
  • Posts: 3853
  • Timelord of the Sith
Re: "Newby" ... there is a flaw in my code... any ideas?
« Reply #11 on: November 29, 2010, 09:22:47 PM »
Pete just go here: http://www.qb64.net/forum/index.php?topic=1740.msg15778#msg15778

You would be hard pressed to get circles (or any other curve for that matter) to look smoother.
Rassilon: My lord Doctor; My lord Master; My lord DarthWho
The Doctor and the master at the same time :WHAT!?!?!

FastMath 1.1.0 released: http://dl.dropbox.com/u/12359848/fastmath.h

codeguy

  • Hero Member
  • *****
  • Posts: 3552
  • what the h3ll did i name that code?
    • stuff at dkm
    • Email
Re: "Newby" ... there is a flaw in my code... any ideas?
« Reply #12 on: November 29, 2010, 09:31:22 PM »
Quote from: mballa01 on November 29, 2010, 09:19:14 PM
codeguy - thanks for the info.  Great coding!  This was an assignment I was tasked with.  So I have to keep it simple, but your code is easy for me to follow and does make more sense.  I'm looking forward to learning more.  This site has been a great learning experience for me.
thank you. i have just seen and participated in far too many "programming courses" where the absolute worst of programming practices are perpetuated. as you visit this site, you'll find a lot of truly talented programmers far better than me, although i did come up with NSpace, my real-time nearly linear speed collision detection algo. i do have a sense of humor, so don't get too upset if i got a little excited about your code. we all had to start somewhere and i did not hone my skills overnight. good coding is like a mondrian (i did an imitation of that too on qb64, just as a joke since someone said my code looked like a mondrian painting). good luck in your studies and remember, programming is also about creativity and thinking for yourself. ;D
http://denteddisk.forums-free.com/make-an-appointment-with-the-resident-code-guru-f34.html

DarthWho

  • Hero Member
  • *****
  • Posts: 3853
  • Timelord of the Sith
Re: "Newby" ... there is a flaw in my code... any ideas?
« Reply #13 on: November 29, 2010, 09:33:34 PM »
sorry about hijacking the topic there a bit
good luck
Rassilon: My lord Doctor; My lord Master; My lord DarthWho
The Doctor and the master at the same time :WHAT!?!?!

FastMath 1.1.0 released: http://dl.dropbox.com/u/12359848/fastmath.h

Mega

  • Sr. Member
  • ****
  • Posts: 295
  • Who GNU?
Re: "Newby" ... there is a flaw in my code... any ideas?
« Reply #14 on: December 01, 2010, 04:03:59 AM »
Here's how I would have gone about it and changed it to make it work efficiently without worrying about unnecessary things.

Code: [Select]
'name, address,foundIt,Msg_Yes,and Msg_No are strings: name of variable+$sign.
'item,quantity,price,size are integers left unchanged. 'sub has been changed to 's' to avoid conflicts/give simplicity
'since QB45 and QB64 have sub as a reserved keyword. Ltrim was added to STR conversions as needed for proper formatting.
'WHILE/WEND to changed to direct FOR statement of exact number of specified elements. If element is seen, foundIt$="Y"
'and EXIT FOR is issued to avoid unnecessary searches/additional cycles.

'declare your stuff:
size = 6: DIM Valid_Item(size), Valid_Item_Price(size):foundIt$ = "":'foundIt$ doesn't have to equal N or anything, as long as it is not Y.

'valid item numbers:
Valid_Item(1) = 106: Valid_Item(2) = 108: Valid_Item(3) = 307: Valid_Item(4) = 405
Valid_Item(5) = 457: Valid_Item(6) = 688

'valid item prices that correspond to numbers:
Valid_Item_Price(1) = 0.59: Valid_Item_Price(2) = 0.99: Valid_Item_Price(3) = 4.50
Valid_Item_Price(4) = 15.99: Valid_Item_Price(5) = 17.50: Valid_Item_Price(6) = 39.00

'ask for information here:
LOCATE 1,1:'put us at the top of the screen
LINE INPUT "Please Enter your Name:", name$: LINE INPUT "Please enter your address:", address$
INPUT "Please enter your item number:", item: INPUT "Please enter your quantity:", quantity

'compare what we found:
FOR s = 1 TO 6: IF item = Valid_Item(s) THEN foundIt$ = "Y": price = Valid_Item_Price(s): EXIT FOR
NEXT s
IF foundIt$ <> "Y" THEN PRINT "Item not found." ELSE txtoutput$ = "Item Found:" + CHR$(13) + STR$(quantity) + " at" + STR$(price) + " each" + CHR$(13) + "Total:$" + LTRIM$(STR$(quantity * price)): PRINT txtoutput$
Just an old-school programmer of covox speech things and such...

  • Print