in reply to Re^5: seeking advice on loops
in thread seeking advice on loops
thanks for all your great advice and wisdom...here is the somewhat finished code. I dont know what use strict is but I could not get the script to run correctly with it. I am sure it is a problem on my end and it is good advice...
I was suppossed to write a subroutine to process the arrays and hashes but I just ended up inserting the foreach loop to do that...not sure how to turn that section into a subroutine with global variables??
#!/usr/bin/perl -w #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>); $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 ($itemname = <STDIN>); push @item, $itemname; print "Please enter the quantity of the item\n"; chomp ($itemquantity = <STDIN>); $qty{$itemname} = $itemquantity; print "Please enter the price of the item (no \$ please)\n"; chomp ($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 foreach $item (@item) { print "Item: $item\n"; $q_item = $qty{$item}; print "Quantity: $qty{$item}\n"; $p_item = $price{$item}; print "Price: \$$price{$item}\n"; $itemtotal = $q_item * $p_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";
also I tried an "if" statement to test the $continue variable for the proper answer but I could not get that to loop until the right answer "Y" or "N" was given so I took that bad code out
I can't tell you how much I appreciate your advice - I am learning slowly!
sierra
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: seeking advice on loops
by Tanktalus (Canon) on Oct 10, 2005 at 22:43 UTC | |
by sierrastar (Sexton) on Oct 11, 2005 at 21:14 UTC | |
by Tanktalus (Canon) on Oct 11, 2005 at 23:22 UTC |