Since this is homework, I'm not going to go rewrite anything. I will, however, help with two things. First is indentation. You need to be consistant - consistant indentation helps so much with readability that I will never stop harping on it. You don't need to necessarily use my indentation style, but you do need to use some indentation style, and you need to use it consistantly. One of the best ways to do this, IMO, is to find a well-publicised style and adopt it. This is because you will always have something to refer to when you're unsure of how to indent a given problem.
The second change is to add use strict. The error messages you're getting are telling you something. One thing it is telling you is that you declared an array @items, but then used the array @item everywhere else. In other words, a typo. In general, typos can lead to incorrect results when you end up using a brand-new global array when you intended to re-use an existing lexically-scoped array instead. In this particular case, it wasn't causing you any actual problems because all of your actual usage of @item was consistant with the sole exception of your declaration. But they have caught problems like that for me, so I continue to ensure all my code, and that of those who work for me, are strict-safe.
Without further adieu, I present the slightly changed code:
#!/usr/bin/perl -w use strict; #cash register and print receipt program my (@items, %qty, %price); #User input here print "Please enter your first and last name\n"; chomp (my $name = <STDIN>); print "Please enter your state sales tax in percentage (i.e. 7.75)\n"; chomp (my $tax = <STDIN>); my $taxrate = $tax / 100; # enter loop to capture customer information here my $continue='Y'; while ($continue eq 'Y'){ print "Please enter an item or product name\n"; chomp (my $itemname = <STDIN>); push @items, $itemname; print "Please enter the quantity of the item\n"; chomp (my $itemquantity = <STDIN>); $qty{$itemname} = $itemquantity; print "Please enter the price of the item (no \$ please)\n"; chomp (my $itemprice = <STDIN>); $price{$itemname} = $itemprice; print "Would you like to add another item? - Y or N\n"; chomp ($continue = <STDIN>); } # process subtotal and print receipt output here my $subtotal; foreach my $item (@items) { print "Item: $item\n"; #$q_item = $qty{$item}; print "Quantity: $qty{$item}\n"; #$p_item = $price{$item}; print "Price: \$$price{$item}\n"; my $itemtotal = $qty{$item} * $price{$item}; print "Item total: \$$itemtotal\n"; $subtotal += $itemtotal; print "Your current subtotal is: \$$subtotal\n"; } #print totals and tax here print "Purchase Subtotal: $subtotal\n"; print "Sales Tax Rate: $tax\n"; my $totaltax = $subtotal * $taxrate; print "Total Sales Tax = \$$totaltax\n"; my $totalamount = $subtotal + $totaltax; print "The total amount of this sale is: \$$totalamount\n"; #print the customer name and current date and time print "\n"; print "Customer: $name\n"; print "\n"; my $now = localtime time; print "Current Date and Time: $now\n";
Now, to your other questions. First, subroutines. Unless that's part of the assignment, I suspect that you shouldn't worry about it too much yet. Until you get down concepts such as variable scope, this may not be an assignment dealing with subroutines. Especially with multiple hashes - to pass them around, you need other concepts such as references. And, in this case, I would be using hashes that contained other hashes rather than having a %qty hash and a %price hash (and even the @items array). And then it would be really easy to pass around and stuff. But until you get there, well, don't cross more than one bridge at a time.
As for your if/loop. If statements don't loop - they branch. And only in one direction: down. What you want is a loop inside your loop. Again, as this is homework, I'm going to give you some pointers, not actual code. So here is what you have:
And this is sort-of what you want to have:print "Would you like to add another item? - Y or N\n"; chomp ($continue = <STDIN>);
Hopefully that helps you get this part down.$continue = some invalid value; while ($continue is not a valid value) { print out the prompt; chomp ($continue = <STDIN>); # unchanged. }
Good luck,
PS - new questions can go in new threads, as this has departed significantly from the original question, and will likely see a larger audience. Keep up the honesty on the homework issue ;-) That said, use your judgement as to whether you're asking new questions or not.
In reply to Re^7: seeking advice on loops
by Tanktalus
in thread seeking advice on loops
by sierrastar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |