Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with a calculation on a hash checkbox

    I am new to Perl and this is my first post on here. I have created a pizza order form in Perl and am having trouble doing the calculations for the total price. I will provide you with the most information possible so you can help the best you can. Thanks in advance!

    instructions:

    Pizza Style : Round or Square
    Programming : Radio Buttons - must choose one and only one option
    Requirements: Optional

    Pizza Size : Small, Medium, or Large
    Programming : Radio Button - must choose one and only one option
    Requirements: must use Array variable to save the values of the radio buttons

    Item selection: User can choose from pepperoni, mushrooms, extra cheese, ham, bacon, green olives, onions, green peppers, pineapple.
    Programming : Checkboxes - can select any combination of items.
    Requirements: must use Hash variable to save the values of the checkboxes

    Delivery selection : Yes or No.
    Programming : Radio Buttons – must choose one and only one option
    Requirements: Optional

    Submit button : when this button is pressed, the cost of the pizza is calculated and displayed in the new Acknowledgment webpage along with all other details.
    Reset button: when pressed resets pizza style to round, size to small, delivery to no, and uncheck any items.

    Cost

    Round or Square
    Small: $3.50 + $1.00/Item
    Medium: $4:50 + $1.50/Item
    Large: $5:50 + $2:00/Item
    Delivery: $4.00

    I am not sure how I can find the number of checkboxes selected and multiply that number by the price of the topping according to the selected pizza price. So far, my output for $price is just the price of the pizza size that was selected and ignores my attempt at calculating toppings.

    Code:
    #!/usr/bin/perl
    #project1.cgi - creates a dynamic Web page that acknowledges
    #the receipt of a pizza order form
    print "Content-type: text/html\n\n";
    use CGI qw(:standard);
    use strict;
    
    #declare variables
    my (@top, $prepare, $size, $deliver, $key, $price, $total, $sm_amount, $med_amount, $lg_amount, $toppings_sm, $toppings_med, $toppings_lg);
    
    my @prepared = ("Round", "Square");
    
    my @sizes = ("Small", "Medium", "Large");
    
    my %toppings = ("P", "Pepperoni",
                    "M", "Mushrooms",
                    "EC", "Extra Cheese",
                    "H", "Ham",
                    "B", "Bacon",
                    "GO", "Green Olives",
                    "O", "Onions",
                    "GP", "Green Peppers",
                    "PI", "Pinapple");
    
    my @delivery_options = ("No", "Yes");
    
    
    #assign input items to variables
    $prepare = param('Prepared');
    $size = param('Size');
    @top = param('Topping');
    $deliver = param('Delivery');
    
    #Calculate pizza cost
    #$sm_amount = "3.50";
    #$med_amount = "4.50";
    #$lg_amount = "5.50";
    #$toppings_sm = "@top{toppings} * 1";
    #$toppings_med = "@top{toppings} * 1.5";
    #$toppings_lg = "@top{toppings} * 2";
    
    if ($sizes[$size] eq "Small") {
            foreach $key (@top){
            $price = $toppings{$key} * 1.00 + 3.50
            }
     
    }
    elsif ($sizes[$size] eq "Medium") {
            foreach $key (@top){
            $price = $toppings{$key} * 1.50 + 4.50
            }
    
    }
    elsif  ($sizes[$size] eq "Large") {
             foreach $key (@top){
            $price = $toppings{$key} * 2.00 + 5.50
            }
    
    
    
    
    }
    
    #create Web page
    print "<HTML><HEAD><TITLE>Pizza Order</TITLE></HEAD>\n";
    print "<BODY><H2>\n";
    print "Thank you, for ordering\n";
    print "our pizza.<BR><BR>\n";
    print "<I>Your pizza has the following specifications:</I><BR><BR>\n";
    print "Style: $prepared[$prepare]<BR><BR>\n";
    print "Size: $sizes[$size]<BR><BR>\n";
    print "Toppings:<BR>\n";
    foreach $key(@top) {
            print "$toppings{$key}<BR>\n";
    }
    print "Delivery: $delivery_options[$deliver]<BR><BR>\n";
    printf "Your total price is \$%.2f.<BR>\n" , $price;
    print "</H2></BODY></HTML>\n";
    I have a bunch of variables quoted out as I tried many different ways to make this work.

    Thanks.
    Last edited by FishMonger; 02-27-2011 at 04:01 PM. Reason: Fixed the code tags

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,872
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Welcome to CF.

    When posting code, please use the regular code tags instead of the icode tags. The code tags will make it easier to read and maintains the formatting.

    Since this is a homework assignment, I can't fix your code for you or give you too many directions on what needs to be done, but I'll do what I can.

    I need to work on something else right now but will come back when I can.

  • #3
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thank You

    Okay sorry about the code tags. I don't want anyone to do my homework for me or anything, I just have been stuck on this for a few days now and my deadline is Wednesday. I appreciate any help or step in the right direction that I can get at this point. if you get to it, that's great. If not, thank you for at least taking the time to read this. Have a good day.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Lindsay View Post
    Okay sorry about the code tags. I don't want anyone to do my homework for me or anything, I just have been stuck on this for a few days now and my deadline is Wednesday. I appreciate any help or step in the right direction that I can get at this point. if you get to it, that's great. If not, thank you for at least taking the time to read this. Have a good day.
    see the man page or perldoc for the cgi module and look for the checkbox_group.
    you can find the size of an array using scalar,

    let me know if you can handle or you need something,

    best regards

  • Users who have thanked oesxyl for this post:

    Lindsay (03-01-2011)

  • #5
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank You so much! What a relief. I was making this way too complicated. You just saved me a whole lot of stress.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Lindsay View Post
    Thank You so much! What a relief. I was making this way too complicated. You just saved me a whole lot of stress.
    you are welcome,

    best regards


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •