
human
New User
Oct 31, 2012, 12:09 AM
Post #6 of 10
(1476 views)
|
Well, I sort of figured it out on my own. Nowhere was I able to find a really easy and satisfying answer to how printf works. I had to look at sites online, Learning Perl (6th Edition), and my teacher's notes. Finally I got it. Anyway, here's the final code that I will be submitting:
#!/usr/bin/perl #Description: # Takes an order for a restaurant, using printf, arrays, and a subroutine. use strict; my @menu=("", "Hamburger", "Frankfurter", "French Fries", "Large Coke", "Medium Coke", "Small Coke", "Onion Rings", "Tally Bill and Start over", "Stop"); # menu array my @foodcost=(undef, 3.49, 2.19, 1.69, 1.79, 1.59, 1.39, 1.19); # price of the food in the menu array my @quantity=(0, 0, 0, 0, 0, 0, 0, 0); # array for amount of food selected my @quantTally=(0, 0, 0, 0, 0, 0, 0, 0); # array to tally up each quantity of food my $v; # control variable my $tally; # a variable to tally up the current bill my $selection; # a variable for what the user selected on the menu # initializing variables... $tally=0; $selection=0; # SUBROUTINE -- SUBROUTINE -- SUBROUTINE -- SUBROUTINE sub menuprint # creating a subroutine for printing out the menu... { # Print out menu print "EMU Gourmet Food\n\n"; for ($v=1; $v<=$#menu; $v++) { printf "$v) $menu[$v] $foodcost[$v]\n"; if ($selection==$v) # if the current value of $v matches the user input { if ($v<8){$quantity[$v]++;} # add up the amount of a given menu item (since 8 and above aren't menu items but rather menu options, they don't get tallied up) } } print "\nEnter selection : "; $selection=<STDIN>; } # SUBROUTINE -- SUBROUTINE -- SUBROUTINE -- SUBROUTINE do # DO STUFF BASED ON USER INPUT { if($selection<8) { &menuprint; } elsif($selection=8) { # here's where the printf fun begins... printf ("\n%-10s%-12s%-10s%-10s\n", "Qty", "Desc.", "Unit \$", "Total"); for ($v=1; $v<=$#menu; $v++) { # adding up the totals for each type of food, then the grand total of everything. $quantTally[$v]+=($foodcost[$v]*$quantity[$v]); $tally+=($foodcost[$v]*$quantity[$v]); if ($quantity[$v]>0) # so only items selected will be printed { printf ("%-10s%-12s%-10s%-10s\n", $quantity[$v], $menu[$v], $foodcost[$v], $quantTally[$v]); } } print "-------------------------------------------------\n"; printf ("%-32s%-10s\n\n", "Total", $tally); $tally=0; # and let's zero everything out. @quantity=(0, 0, 0, 0, 0, 0, 0, 0); @quantTally=(0, 0, 0, 0, 0, 0, 0, 0); &menuprint; } } while($selection!=9); print "\nEnjoy your meal";
|