Help with Invoice Program

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

Help with Invoice Program

Post by flirt85 »

I'm also trying to create a program that outputs data into an invoice format. Any suggestions?


CLS
DIM item AS STRING 'item sold
DIM specs AS STRING 'specifications of item
DIM price AS SINGLE 'price of item
DIM subtotal AS SINGLE 'subtotal of items purchased
DIM tax AS SINGLE 'sales tax of items purchsed
DIM total AS SINGLE 'total price including tax
DIM h1 AS STRING 'heading 1
DIM h2 AS STRING 'heading 2
DIM x AS INTEGER 'loop variable
DIM datastring AS STRING 'data print string
DIM ct AS SINGLE 'counter

DATA "Computer","102GHz",835.89
DATA "Monitor"."17 inch",189.98
DATA "Printer","Color",256.23
DATA "Scanner","Flatbed",145.95
DATA "Speakers"."Stereo".32.27
DATA "Software","Office",387.77
DATA "xxx","xxx",-1

h1 = "Item Specs Cost"
h2 = "----- ----- ----"

PRINT h1 'print headings
PRINT h2

total = 0 'set total to 0
tax = 0 'set tax to 0
subtotal = 0 'set accumulator to 0
ct = 0 'set counter to 0
datastring = "\ \ \" 'set format string


READ item, specs, price
WHILE ct <= 5
subtotal = subtotal + price
ct = ct + 1

WEND
tax = 12.5 * subtotal
total = subtotal + tax

PRINT USING datastring; item; specs; price
LOCATE 8, 30
PRINT subtotal
LOCATE 9, 30
PRINT tax
LOCATE 10, 30
PRINT total
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Hi Flirt85,
You seem to be a pretty good programmer, judging from your code. Having looked at your inventory program also, I notice some things that I consider bad habits.
1) You seem to prefer processing input data directly out of DATA statements. I prefer to set up one or more arrays, and READ the DATA into the arrays, and then process the arrays. It''s a lot cleaner for me. However, in this particular simple program, your approach is acceptable since you are only making one pass over the data.

2) Your count CT goes from 0 to 5, which is confusing. You have 6 sets of data so you should really count from 1 to 6. Again, this is my preference.

3) You have "padding" data of "xxx" as the last DATA statement, which you don't need for this program.

3) The printing of your subtotal, tax, and total should line up, and be rounded to 2 decimal places, as follows.
MASK$="#,###.##"
PRINT USING MASK$;SUBTOTAL
PRINT USING MASK$;TAX
PRINT USING MASK$;TOTAL

Otherwise, your program looks good and should run.
*****
flirt85
Coder
Posts: 13
Joined: Sun Mar 12, 2006 2:14 pm

Invoice Program

Post by flirt85 »

Thank you Moneo. This program is becoming quite a pain. It runs as is, but only displays the first set of data....six times. I can see the PRINT MASK as an improvement, but my WHILE statement really needs help. Do you have any more suggestions?
User avatar
Theophage
Coder
Posts: 44
Joined: Sun May 07, 2006 7:32 pm
Location: Tucson, AZ
Contact:

Post by Theophage »

well, it looks like you either need arrays for your item, specs, and price variables, or you need to put your print statements inside your WHILE / WEND loop...
Daniel "Theophage" Clark
theophage (at) geocities (dot) com

"God used to be my co-pilot, but our plane crashed in the mountains and I had to eat Him..."
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Re: Invoice Program

Post by moneo »

flirt85 wrote:Thank you Moneo. This program is becoming quite a pain. It runs as is, but only displays the first set of data....six times. I can see the PRINT MASK as an improvement, but my WHILE statement really needs help. Do you have any more suggestions?
It's not clear what level of subtotals and total you want. Let's assume that subtotal is the sum of all 6 prices, and tax is total tax for all 6 items, and total is the grand total.

If this were truly an invoice, you would want to show the price, tax, and extension for each item, with totals at the end, which of course must crossfoot. We'll do that next time.

The READ item,specs,price is outside of the WHILE loop, so you'll always be working with the same item, the first. You need to relocate the WEND so it covers that calculations for all 6 items, prints each of the 6 items, and then at the end, prints out only the subtotal, tax, and total. Here's you're original code:

Code: Select all

READ item, specs, price
WHILE ct <= 5
subtotal = subtotal + price
ct = ct + 1
WEND
tax = 12.5 * subtotal
total = subtotal + tax

PRINT USING datastring; item; specs; price
LOCATE 8, 30
PRINT subtotal
LOCATE 9, 30
PRINT tax
LOCATE 10, 30
PRINT total
Here's my suggested code:

Code: Select all

WHILE ct <= 5
READ item, specs, price
subtotal = subtotal + price
ct = ct + 1
PRINT USING datastring; item; specs; price
WEND

tax = 12.5 * subtotal
total = subtotal + tax

LOCATE 8, 30
PRINT subtotal
LOCATE 9, 30
PRINT tax
LOCATE 10, 30
PRINT total
*****
flirt85
Coder
Posts: 13
Joined: Sun Mar 12, 2006 2:14 pm

syntax error on READ and Type Mismatch on Print Using

Post by flirt85 »

I made the modifications to the code that you suggested, but on the READ statement inside of the loop, I recieve a Syntax error. Also, I have rarely been able to actually use the PRINT USING datastring because of the type mismatch error.....??
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Hi Flirt85, I thought you had disappeared.

Regarding the READ error, take a look at this DATA statement which has a period instead of a comma between the first 2 fields, which is probably the cause.

DATA "Monitor"."17 inch",189.98

As far as the PRINT USING is concerned, I never use it like you do for formatting data fields, just for formatting values with dollar signs, commas, decimal points.
I think you're getting a type mismatch error because you're mixing string and numeric data.

Instead if using the PRINT USING, why don't you use your own formatting technique by putting, let's say, 2 spaces between the fields. Try it. It might not be as pretty, but it will certainly work without giving an error.
*****
flirt85
Coder
Posts: 13
Joined: Sun Mar 12, 2006 2:14 pm

Post by flirt85 »

Good afternoon Moneo,
Between work and school, it takes me awhile to get online =) I'm still getting a READ error after I fixed the period into a comma in my data. I found another period and fixed it as well. I don't get it?
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

flirt85 wrote:......
I'm still getting a READ error after I fixed the period into a comma in my data. I found another period and fixed it as well. I don't get it?
If you have more than one constant (string or numeric) on a DATA statement, then you must delimit (separate) each of these with a COMMA. I just noticed that the DATA statement for "Speakers" has 2 periods instead of commas. There's nothing special to understand. That's just the rule for DATA statements.
*****
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Flirt85,

A good way to be able to see the information and the delimeters in a DATA statement more clearly is to line up and spread out the fields. Example:

Code: Select all

DATA "Computer" , "102GHz"   ,  835.89
DATA "Monitor"  , "17 inch"  ,  189.98
DATA "Printer"  , "Color"    ,  256.23
etc....
While doing this, I just noticed that "Monitor" was delimited with a period instead of a comma. When you spread it out, it becomes more visible.
*****
Post Reply