Ask your own question, for FREE!
Computer Science 10 Online
OpenStudy (anonymous):

I would really appreciate help with a QuickBasic program my teacher assigned us for homework. However, my code just will not work. I'm slow to understand so please be descriptive and specific. And please just give me a hand!

OpenStudy (rsmith6559):

What's your code? What's going wrong?

OpenStudy (anonymous):

Well, first you need to understand what the instructions were: Write a program to assist a shopkeeper to price his goods. The prices are set according to ranges of cost of the products (the ranges are: $0-$9.99, $10-$19.99, >$20), each range having a markup percentage that changes from one shopping season to another. So, the program needs the current markups for the cost ranges (put into an array), the product names and their costs. These are supplied in the form of a file of information created by the shopkeeper, which will be read by the program. (See the products.dat file for an example of the data file.) The program will then print a table where each row holds the product name, the cost, the markup and the price (in columns – will require formatted writes). 1. Analysis: I. Input? Markups, products and their costs. How to find the price. II. Output? Echo the markups in a suitable format: a table with one product per row, showing name of product, cost, markup and price. III. Processing – Read the input file, storing the markups, then repeatedly read the name and cost of a product and determine its markup range; print a table row with the name (at least 15 characters), cost, calculated markup and price (cost + markup), until there are no more products. 2. Pseudocode: [ create ID block ] declare array (markups) open data file print program header read markups from file into an array print table headings loop until end-of-file read product name and cost from the file if cost < $10 then profit = cost * markups(1) else if cost <$20 then profit = cost * markups(2) else profit = cost * markups(3) end if price = cost + profit print product table row: name, cost, profit, price end loop stop The products.dat file contained the following: .06 .08 .12 "Dirt Dobber Drops" 2.24 "Mulberry Muffin" 3.10 "Cranberry Cake" 16.75 "Persimmon Parfait Party Pack" 24.55 And finally, here is MY CODE: REM **************************************************** REM Assignment: QuickBasic 4 Program REM DIM markups(3) AS SINGLE OPEN "E:\products.dat" FOR INPUT AS #1 PRINT "This program will assist the user in pricing goods." FOR n = 1 TO 3 READ markups(n) NEXT n PRINT "Product" PRINT "Cost" PRINT "Markup" PRINT "Price" DO WHILE (NOT EOF(x)) INPUT #1, product$ INPUT #1, cost IF cost < 10 THEN profit = cost * markups ELSE profit = cost * markups END IF price = cost + profit PRINT USING "\ \"; product$ PRINT USING "##.##"; cost PRINT USING "##.##"; profit PRINT USING "##.##"; price LOOP STOP

OpenStudy (rsmith6559):

I haven't done BASIC in 25 years, but IMO, your references to the markups array each need an index to which element of the array that you want. Your if construct also needs and else if block. You have 3 markup ranges. If you can use whitespace freely, you can make this easier to read, understand and debug.

OpenStudy (anonymous):

I'm so lost, though. My teacher isn't exactly the best teacher in the world. I've been to so many sites, it's ridiculous. But thanks for the pointers!

OpenStudy (rsmith6559):

Do you understand what I was saying about else if and indexes?

OpenStudy (anonymous):

I understand else if, but I think I do have an else if in my program. Indexes, though, make no sense to me! Sorry.

OpenStudy (anonymous):

What is the expected result and what are you getting? What is this code meant to do: IF cost < 10 THEN profit = cost * markups ELSE profit = cost * markups END IF Note how both statements are identical

OpenStudy (rsmith6559):

READ markups(n) This is from your code. "n" is acting as an index. The first time through, you were storing into markups(1). When you want to access that value, you need to refer to it the same way. In your code, you're refering to markups, the whole array. BTW, you have an if, and you have an else. That's only two of the three price ranges.

OpenStudy (anonymous):

That part of the code doesn't agree with your pseudocode. markups refers to the whole array markups(1) to the first element markups(2) to the second etc.

OpenStudy (anonymous):

(which is what rsmith meant by "index")

OpenStudy (anonymous):

Would this work? DO WHILE (NOT EOF(x)) INPUT #1, product$ INPUT #1, cost IF cost < 10 THEN profit = cost * markups(1) ELSE IF cost < 20 THEN profit = cost * markups(2) ELSE profit = cost + profit END IF

OpenStudy (anonymous):

Also I changed to dim markups(3) as single, but how do I get it to read the file as stated in the directions?

OpenStudy (rsmith6559):

You're close. I'm not happy with what happens after the else, but other than that, YES.

OpenStudy (anonymous):

So you think it would work if I fix those things? It will actually read the data from the products.dat file into the array?

OpenStudy (rsmith6559):

I thought you were reading it according to the directions. This is where the years show up.

OpenStudy (rsmith6559):

I would expect it to.

OpenStudy (anonymous):

INPUT #1, product$ INPUT #1, cost reads from the file, as far as I can tell.

OpenStudy (anonymous):

How do I read the markups from the file into the array? That's one of the first steps, and I'm not sure how to read those decimals out of the products.dat file I pasted on here.

OpenStudy (anonymous):

ah, but you're supposed to read the 3 values from the top of the file. .06 .08 .12 Presumably READ(...) doesn't read from the file, but INPUT #1 does. So you could do INPUT #1, markups(i) instead of the READ

OpenStudy (anonymous):

So just say: input #1, markups(1) input #1, markups(2) input#1,markups(3)

OpenStudy (anonymous):

either that, or just do it in the loop, like you're doing right now, and just replace the READ markups(n) with INPUT #1, markups(n)

OpenStudy (anonymous):

Okay, I'll try it and let you guys know if it works like it's supposed to!

OpenStudy (rsmith6559):

Now is the time to introduce you to "unit testing". Instead of writing the whole program and then trying to debug it all at once, just write a small section and test it. Copy: DIM markups(3) AS SINGLE OPEN "E:\products.dat" FOR INPUT AS #1 FOR n = 1 TO 3 READ markups(n) NEXT n into another file, and try having it print out the values. Try doing some math with the values too, to make sure that they're being interpreted as numbers.

OpenStudy (anonymous):

It's saying the path is not found. How do I make it find the products.dat file. It's on my flash drive (not in any folder, just on the drive)!

OpenStudy (rsmith6559):

What drive letter is the flash drive?

OpenStudy (anonymous):

E

OpenStudy (anonymous):

what is the file called on the flash drive?

OpenStudy (anonymous):

presumably E:\products.dat should find the file, if the file is called products.dat and is directly in the root directory of E (and the flash drive is connected).

OpenStudy (anonymous):

try putting it on C: and changing the line OPEN "E:\products.dat" FOR INPUT AS #1 to OPEN "c:\products.dat" FOR INPUT AS #1

OpenStudy (anonymous):

I think I fixed it. Thanks for the concern though.

OpenStudy (anonymous):

K

OpenStudy (rsmith6559):

More importantly, do you understand what we've been doing?

OpenStudy (anonymous):

I do understand, but my program is saying "OUT OF DATA". It's not really reading the markups still.

OpenStudy (anonymous):

sry, gotta go, but rsmith got you covered. bye

OpenStudy (anonymous):

Thanks, nczempin for all the help!

OpenStudy (rsmith6559):

Is this the whole program, or that unit test?

OpenStudy (anonymous):

Just the unit test.

OpenStudy (rsmith6559):

Okay, that's looking for three pieces of data, and that message says that the file doesn't have three pieces of data. Inside the for loop, add a print statement to print out what has just been stored. Something like: FOR n = 1 TO 3 READ markups(n) print markups(n) NEXT n

OpenStudy (anonymous):

DIM markups(3) AS SINGLE OPEN "E:\products.dat" FOR INPUT AS #1 PRINT "This program will assist the user in pricing goods." FOR n = 1 TO 3 READ markups(n) PRINT markups(n) NEXT n INPUT #1, markups(1) INPUT #1, markups(2) INPUT #1, markups(3) This is what I've got but it's still saying OUT OF DATA.

OpenStudy (rsmith6559):

I was expecting it to say: .06 .08 .12 I just Googled Quick Basic, and I'm seeing variables referred to as: variable$ I don't know BASIC well, is that dollar sign needed?

OpenStudy (anonymous):

$ stand for strings so I didn't think a numerical value would require a $.

OpenStudy (rsmith6559):

What about % ?

OpenStudy (anonymous):

I think that is only for integers.

OpenStudy (anonymous):

! or no symbol stands for a decimal.

OpenStudy (rsmith6559):

In the whole program, where it says EOF(x), that should be EOF(1), so that it refers to your file.

OpenStudy (anonymous):

Okay, well I'll try to write it out. Let you know in a few minutes!

OpenStudy (rsmith6559):

I'm bouncing between this tab and another with http://voices.yahoo.com/using-quickbasic-open-command-530801.html on it. They're doing just about the same thing. Here's their code: OPEN "A:\EXAMPLE.DAT" FOR INPUT AS #1 DO UNTIL EOF(1) INPUT #1, B$ LOOP CLOSE #1 so I would expect that to be like: OPEN "E:\products.dat" FOR INPUT AS #1 FOR n = 1 TO 3 input #1, markups(n) print markups(n) NEXT n close #1 For the unit test.

OpenStudy (anonymous):

I tried it out with this code: REM Assignment: QuickBasic 4 Assignment REM DIM markups(3) AS SINGLE OPEN "E:\products.dat" FOR INPUT AS #1 PRINT "This program will assist the user in pricing goods." FOR n = 1 TO 3 READ markups(n) PRINT markups(n) NEXT n CLOSE #1 INPUT #1, markups(1) INPUT #1, markups(2) INPUT #1, markups(3) PRINT "Product" PRINT "Cost" PRINT "Markup" PRINT "Price" DO WHILE (NOT EOF(1)) INPUT #1, product$ INPUT #1, cost IF cost < 10 THEN profit = cost * markups(1) ELSEIF cost < 20 THEN profit = cost * markups(2) ELSE profit = cost * markups * (3) END IF price = cost + profit PRINT USING "\ \"; product$ PRINT USING "##.##"; cost PRINT USING "##.##"; profit PRINT USING "##.##"; price LOOP STOP The output screen was 0.00 and only had Price, Markups, Cost, etc. listed, but not the names of the goods at the store.

OpenStudy (rsmith6559):

It probably did, it was just a null string( "" ). The close command belongs between the loop and stop commands. As written, the file was closed before the do while loop.

OpenStudy (anonymous):

I put the close#1 as the do while loops, and it's still saying OUT OF DATA and only showing 0.00 and the price, cost, profit, and markup labels.

OpenStudy (rsmith6559):

Okay this has taken a big detour. I looked over what you posted, and there are other issues. I don't think that that read statement in the for loop is any good. I think that that should be: input #1, markups(n) The three input #1 statements just before PRINT "product" should be deleted.

OpenStudy (anonymous):

Here is the output screen now: This program will assist the user in pricing goods. .06 .08 .12 Product Cost Markup Price Dirt Dobber Drops 0.00 0.00 0.00 Muffin" 3.10 0.00 0.00 0.00 Cake" 16.75 0.00 0.00 0.00 So, basically, the goods are being cut down and the totals aren't being entered it. Also, it doesn't exactly look like a readable table. I'm so sorry you're having trouble with this, but I really appreciate your help so much!

OpenStudy (rsmith6559):

Actually, this is good. The .06 .08 .12 are from that print command we put in the for loop. That can be deleted, it was only for debugging. The Product, Cost, Markup, Price lines are because of the separate print statements. Each print statement will print a line. This is also why each item is printing many lines. In your else clause, there's an asterick between markups and (3), which shouldn't exist. Delete it. That just leaves us with the zeros to deal with. My instict is that we don't have our data types right. Actually, that print statement in the for loop, can that be changed to something like: print markups(n) * 3 ? Remember, I don't know BASIC specifically. I'd have to hook up my Commodore 64 to get a BASIC interpreter. (no joke)

OpenStudy (anonymous):

I changed my code to: FOR n = 1 TO 3 INPUT #1, markups(n) PRINT markups(n) * 3 NEXT n PRINT "Product", "Cost", "Markup", "Price" DO WHILE (NOT EOF(1)) INPUT #1, product$ INPUT #1, cost IF cost < 10 THEN profit = cost * markups(1) ELSEIF cost < 20 THEN profit = cost * markups(2) ELSE profit = cost * markups(3) END IF price = cost + profit PRINT USING "\ \"; product$ PRINT USING "##.##"; cost PRINT USING "##.##"; profit PRINT USING "##.##"; price LOOP CLOSE #1 STOP The output screen is now: .18 .24 .36 Product Cost Markup Price Dirt Dobber Drops 0.00 0.00 0.00 Muffin" 3.10 0.00 0.00 0.00 Cake" 16.75 0.00 0.00 0.00

OpenStudy (rsmith6559):

Cool. We now know that our markups array contains three numbers. Otherwise, an error would have happened. I was looking at a tutorial, I'm not sure how old it is, http://www.petesqbsite.com/sections/tutorials/zines/qbcm/13-verybasics.html that talks about using an exclamation point for real numbers.

OpenStudy (rsmith6559):

Actually, at this point, we should check our inputs inside the do while loop. After reading in the two variables, print them out the same way that we did in the for loop. That will test the cost variable to make sure that it's a number too.

OpenStudy (anonymous):

Does the way I have the code at the end not work? I used the PRINT USING format. Should I change that and instead put print product$, cost after the two input lines.

OpenStudy (rsmith6559):

That could be a problem, I don't know. That's the kind of BASIC-specific thing that I don't know. I mostly program in Python and C++. I did program in BASIC on my Commodore 64 in the '80s.

OpenStudy (anonymous):

I'm so sorry, but I have to go. Thank you so much for giving up hours of your time to help me. I think I have a better handle now on the problem...thanks to you! I'm just too tired to go on right now. I need to take a breat from QB, but I will hopefully finish tomorrow. If you happen to come up with anything, you can email me at tishlucas89@yahoo.com. However, you've helped me so much that I think you should just forget all about this! But seriously, thank you. I feel better than I did and will hopefully get this done soon! Thanks again!

OpenStudy (rsmith6559):

Glad to help. This is what and why incremental development is a lot less frustrating that trying to do the whole thing at one time.

OpenStudy (anonymous):

it looks like the product names and costs are not read correctly. It seems to work on the first product "Dirt ...'" but not on the others. insert some PRINT statements directly after the INPUT statements, like you did before. My guess is that the first cost is not read correctly, and things go downhill from there. Perhaps you need to tell the interpreter/compiler what kind of data type is? E. g. "SINGLE" (which I assume means "single precision"), like you did on the array. The USING clause presumably just formats the data you're printing (so that you don't have 20 digits after the decimal point, for example). What you can also do is just set "cost" to a constant value (e. g. 2.4) just after the INPUT, for testing purposes: then you should be able to determine whether the rest of your program is correct.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!