CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Hopelessly stuck on an assignment

 



human
New User

Oct 29, 2012, 5:44 PM

Post #1 of 10 (2053 views)
Hopelessly stuck on an assignment Can't Post

Hello everybody,

I have this program due Wednesday, and I'm just totally lost. I figured I would see if someone online can throw me a line here.

Some background: this is an intro college course. The assignment instructions are here: http://www.emunix.emich.edu/~sverdlik/COSC146/PerlAssign4.html

here is my code at the moment. It's a hot mess, I know. I've spent all day on this, I'm obviously not inclined toward this sort of thing. Please, be a good samaritan!

Code
#!/usr/bin/perl 
# First Name: ----
# Last Name: ----
# ID: ----
# Assignment #4
#
#Description:
# Takes an order for a restaurant, using printf, arrays, and a subroutine.

use strict;
my @menu; # menu array
my @foodcost; # price of the food in the menu array

my @quantity; # required array for assignment 4. The point of this
#array is to hold the number of an item ordered (i.e. $quantity[0] represents
#the number of hamburgers ordered, $quantity[1] represents the number of
#frankfurters ordered, etc.)

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

$tally=0; # initializing variables...
$selection=0;
@quantity=0;
print "quantity = @quantity\n";
# give values to the menu...
@menu=("", "Hamburger", "Frankfurter", "French Fries", "Large Coke", "Medium Coke", "Small Coke", "Onion Rings", "Tally Bill and Start over", "Stop");
# ... and coinciding prices.
@foodcost=(undef, 3.49, 2.19, 1.69, 1.79, 1.59, 1.39, 1.19);


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 selection = the menu item, tally it.
{
$tally+=$foodcost[$v];
}
}
}

&menuprint;

# now, for user input:
do
{
print "\nEnter selection : ";
$selection=<STDIN>;

if($selection<8)
{
print "\nEnter quantity : ";
$quantity[$selection]=<STDIN>;

&menuprint;
}
elsif($selection=>8)
{
&menuprint;
#print "Your total bill is : \$$tally\n";
print "Qty\t Desc.\t Unit \$\t Total\n";
for ($v=1; $v<=$#menu; $v++)
{
if ($quantity[$v]>0)
{
printf "quantity $quantity[$v]\nmenu $menu[$v]\t%-7.2f foodcost $foodcost[$v]\tfoodcost times quantity $foodcost[$v]*$quantity[$v]\n-------------------------------------------------\nTotal\t\t\t";

for($v=0;$v<$#quantity;$v++) # gotta add up the total
{
$tally+=($quantity[$v]*$foodcost[$v]);
print "tally $tally plus equals quantity $quantity[$v] times foodcost $foodcost[$v]\n";
}

printf "\$$tally\n";

@quantity=0;
$v=0;
}
}
$tally=0;
}
} while($selection<9);

print "\nEnjoy your meal";



rovf
Veteran

Oct 30, 2012, 2:02 AM

Post #2 of 10 (2048 views)
Re: [human] Hopelessly stuck on an assignment [In reply to] Can't Post

Since this is an assignment, I won't, of course, present a complete solution, but give you hints where to look at.

But, first of all, please be more specific: What is your actual problem (i.e. where did you get stuck)?

A few remarks after glancing over your program:

(1) It's great that you use strict, but please also use warnings. I personally go a step further with my programs and even have usually a


Code
use warnings  FATAL => qw(all);


in my code (which makes my program die if it encounters a warning situation). Removing all the warnings in many cases also removes bugs.

(2) Don't use the '&' notation in order to call your subroutines, unless you know exactly what you are doing. In this program, it doesn't make a difference, but better you do it right from the beginning. Hence, call your function using



Code
menuprint();


(3) Your assignment


Code
@quantity=0;


stores an array of length 1 into @quantity. Is this really what you want? Later you extend the array, but in general, you don't extend it so far that it becomes the same size as @menu. For this, you would have to write $quantity[$#menu]=0. Still, this would leave the remaining array elements undef.

(4) I don't think that


Code
$selection=>8


is doing what you expect. Have a look at the perlop page (i.e. type on the commandline perldoc perlop to see the meaning of the => operator).


Laurent_R
Veteran / Moderator

Oct 30, 2012, 12:04 PM

Post #3 of 10 (2043 views)
Re: [human] Hopelessly stuck on an assignment [In reply to] Can't Post

Yes, tell us where you are encountering problems.

A couple of advice:
- make your variables local to the block where they belong, instead of declaring them at the top of the file
- Avoid using the C-style loop, use rather the Perl for or foreach construct
- Use meaningful names for your variables

An example of each of these advice in one line:

[code]for ($v=1; $v<=$#menu; $v++) {
[/code]
is better written:

[code]foreach my $dish_nr (1 .. $#menu) {
[/code]


human
New User

Oct 30, 2012, 4:14 PM

Post #4 of 10 (2033 views)
Re: [Laurent_R] Hopelessly stuck on an assignment [In reply to] Can't Post

Thank you guys for your input about better practices. For this program, the final product must look just like this:

Code
Qty       Desc.       Unit $    Total 
2 Hamburger 3.49 6.98
1 Large Coke 1.79 1.79
-------------------------------------------------
Total 8.77

I think I am most puzzled about the "quantity" array I have to use. This program is actually an advanced version of an earlier one. In that one, I had a variable called $tally that would simply add the total up each time the user added an item. Now I tried to figure out a way to tally up the total after a user selects items and the quantity they want... and it also has to be formatted with printf to look just right :(

I looked at the instructions again - apparently, I can get away with not using the @quantity array in such a tricky manner. I'll just have it add one each time the user selects the given item. That'll make things a bit more manageable. Still doesn't totally resolve the printf formatting, though.


(This post was edited by human on Oct 30, 2012, 4:29 PM)


FishMonger
Veteran / Moderator

Oct 30, 2012, 5:46 PM

Post #5 of 10 (2031 views)
Re: [human] Hopelessly stuck on an assignment [In reply to] Can't Post

You need to read the documentation for the printf and sprintf functions to learn how they are used.

http://perldoc.perl.org/functions/printf.html
http://perldoc.perl.org/functions/sprintf.html


human
New User

Oct 31, 2012, 12:09 AM

Post #6 of 10 (2023 views)
Solution [In reply to] Can't Post

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:

Code
#!/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";



rovf
Veteran

Oct 31, 2012, 2:51 AM

Post #7 of 10 (2021 views)
Re: [human] Solution [In reply to] Can't Post


Quote
Nowhere was I able to find a really easy and satisfying answer to how printf works.


On the commandline, type:

perldoc -f printf

Let us know if there is anything which you don't understand from the printf documentaion....


rovf
Veteran

Oct 31, 2012, 2:55 AM

Post #8 of 10 (2020 views)
Re: [human] Solution [In reply to] Can't Post

BTW, I don't think this statement is correct:


Code
elsif($selection=8)


This assigns 8 to $selection. Have a look again to perlop to understand the operators.

BTW, if you had followed my advice and had put "use warnings;" in your code, Perl would already have told you this particular error....


Laurent_R
Veteran / Moderator

Oct 31, 2012, 5:35 AM

Post #9 of 10 (2015 views)
Re: [rovf] Solution [In reply to] Can't Post


In Reply To
BTW, if you had followed my advice and had put "use warnings;" in your code, Perl would already have told you this particular error....


The OP seems to have decided NOT to follow any of the advice given to him or her. It is probably best not to give further advice, as this is evidently a waste of time.


human
New User

Nov 1, 2012, 2:59 PM

Post #10 of 10 (1998 views)
Re: [Laurent_R] Solution [In reply to] Can't Post

People people, it's not that I wasn't taking your advice, it's that it was due in a few hours and I didn't have time to learn how to implement your best practices. I thank you for your advice, really I do.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives