Here was an idea I had to add the quantity to the %grocerytotals hash. Somewhere in here I'm off a bit when I try to print it
foreach my $element (keys%cart){ print "\n$element\n"; $grocerytotals{$count} = $count + $grocerytotals{$coun +t}; print "$grocerytotals{$count} items added to current t +otal"; }

I'm trying to write a program for my class which is a grocery purchasing program. First, its supposed ask the user how much money they can spend at the grocery store. Then, read items they want to purchase and how many of each item. If they enter the same item multiple times, then it should add that to the current total for the item.

My problem is that I can't figure out how get it to add the item to show how many items they have purchesed and list that in the output. Since the user can add an item multiple times I need to take that into consideration as well. If anyone can tell me how to go about doing this with out giving me the out right answere (since I need to learn this stuff), I would be very gratefull.

Thanks
#!/usr/local/bin/perl use strict; my %grocerylist = (Eggs => '1.99', Milk => '1.75', Cookies => '2.99', Carrots => '1.25', Eggplant => '1.89', Cereal => '3.25', Gum => '0.75', Juice => '1.99'); my %grocerytotals = (Eggs => '0', Milk => '0', Cookies => '0', Carrots => '0', Eggplant => '0', Cereal => '0', Gum => '0', Juice => '0'); my %cart = (); my $action; my $cash; my $count; my $quantity = 0; my $total; my $temp; my $newcount; print "Welcome to the Acme(tm) grocery store.\n"; print "How much do you have to spend today?\n"; chomp ($cash = <STDIN>); printf("%-1s \$%.2f\n", "You have:", "$cash\n"); print "\nEnter an item name, 'cart' to view your current purchases,\n" + . "or 'showall' to show all available items.\n"; chomp ($action = ucfirst<STDIN>); while ($action){ if ($action eq 'Showall'){ #no code written here yet }elsif ($action eq 'Cart'){ my $purchase; print "-" x 25; printf("%-15s %s\n", "\nItem", "Quantity"); foreach my $element (keys%cart){ if ($cart{Eggs}){ printf ("%-15s %s\n", "\n$element", "$grocerytotals{0} +", ); } } # show how much money is left printf("\nCash left \$$cash\n"); print "\nEnter an item name, 'cart' to view your current purch +ases,\n" . "or 'showall' to show all available items.\n"; chomp ($action = ucfirst<STDIN>); if ($action eq 'Q'){ &quit(); } next; }elsif ($action ne 'Showall' or $action ne 'Cart'){ if ($action eq 'Q'){ &quit(); } my $purchase = $action; print "How many?\n"; chomp ($count = <STDIN>); #validate item if (exists $grocerylist{$purchase}){ $cart{$purchase} = $grocerylist{$purchase}; printf("%-15s %s\n", "You purchased $purchase", $groceryli +st{$purchase}); }else{ print "Sorry we don't stock $purchase! Please choose an i +tem from the list\n"; chomp ($purchase = ucfirst<STDIN>); if ($purchase eq 'Q'){&quit(); } } $quantity = $count + $quantity; $grocerytotals{0} = $quantity; print "adding $grocerytotals{0}\n"; #still need to calculate if we have enough to buy more $cash -= ($grocerylist{$purchase} * $count); print "\nEnter an item name, 'cart' to view your current purch +ases,\n" . "or 'showall' to show all available items.\n"; chomp ($action = ucfirst<STDIN>); if ($action eq 'q'){ &quit(); } next; } }

Janitored by Arunbear - added readmore tags, as per Monastery guidelines


In reply to Need help on part of homework by gitarwmn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.