Solved

Per sub routine

Posted on 2011-09-15
6
251 Views
Last Modified: 2012-05-12
I am working on a perl script that would print a value based on a menu option. Below is the menu option.
    Select one of:
    1. Print the Current Value
    2. Add a number to current value
    3. Subtract a number from the current value
    4. Multiply the current value by a number
    5. Divide the current value by a number
    6. Raise the current value to a power
    7. clear the current value to 0
    8. Exit";

the problem i am having is with the loop. I want it loop until the user enter 8 to exit.
#!/usr/bin/perl
    #use strict;
    use warnings;





    # Subroutine prototypes
    sub do_exit();
    sub current_value();
    sub add_number();
    sub subtract_number();
    sub multiply_number();
    sub divide_number();
    sub raise_number();
    sub clear_number();
$value=0;


    # Print the menu selection
    print "
    Select one of:
    1. Print the Current Value
    2. Add a number to current value
    3. Subtract a number from the current value
    4. Multiply the current value by a number
    5. Divide the current value by a number
    6. Raise the current value to a power
    7. clear the current value to 0
    8. Exit";
print "\n";


    # Get the user's input
 $menu_item = <>;
while ($menu_item ne 4) {
    chomp($menu_item);
    # Define the actions to take
    if ($menu_item == 1) 
	{
        current_value();           
   	 } 
	elsif ($menu_item == 2) 
	{
        add_number();
	}
	elsif ($menu_item == 3) 
	{
        subtract_number(); 
	  }
     	elsif ($menu_item == 4)
	 {
       multiply_number(); 
	}
  	 elsif ($menu_item == 5) 
	{
       divide_number(); 
	}
   	elsif ($menu_item == 6) 
	{
       raise_number(); 
	}
 	elsif ($menu_item == 7) 
	{
        clear_number(); 
	}
	 elsif ($menu_item == 8) {
         do_exit();
	}

     else {    
        print "I didn't understand the command.\n";
        do_exit();
    }

    exit 0;

} 
   

    #-------------------------------------------------------------
    sub current_value() {
		
	print $value;    
	print "\n";
	return;
    }

    sub add_number() {
	print "enter number to add,\n";
	$number = <STDIN>;
 	$value = ($number +$value);
 	print $value,"\n";
        return;
    }
    
     sub subtract_number() {
	print "enter number to subtract,\n";
	$number = <STDIN>;
 	$value = ($value-$number);
 	print $value,"\n";
        return;
    }

	  sub multiply_number() {
	print "enter number to multiply,\n";
	$number = <STDIN>;
 	$value = ($value*$number);
 	print $value,"\n";
        return;
    }

	sub divide_number() {
	print "enter number to divide,\n";
	$number = <STDIN>;
	if ($number ==0){
	print "please choose number greater than 0, \n";
}
 	else {
	$value = ($value/$number);
 	print $value,"\n";
}
        return;
    }
	  sub raise_number() {
	print "enter number to multiply,\n";
	$number = <STDIN>;
 	$value = ($value**$number);
 	print $value,"\n";
        return;
    }
	  sub clear_number() {
 	$value = (0);
 	print $value,"\n";
        return;
    }
    sub do_exit() {
        print "Exiting...\n";
        return;
    }

Open in new window

0
Comment
Question by:SheldonC
  • 2
  • 2
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
jeromee earned 500 total points
ID: 36545112
Your do_exit() should call the function exit() instead of returning...
Good luck!
0
 

Author Comment

by:SheldonC
ID: 36545183
I am not quite sure I quite understand
0
 
LVL 10

Expert Comment

by:jeromee
ID: 36545277
Replace the code at line138 from:
sub do_exit() {
        print "Exiting...\n";
        return;
    }

Open in new window

to
sub do_exit() {
        print "Exiting...\n";
        exit(1);
    }

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:SheldonC
ID: 36545292
sorry,thanks I got it.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 36545356
1) re-enable strict and declare all of your vars.  The combination of the strict and warnings pragmas will catch lots of problems that might otherwise be missed and could be difficult to track down.

2) get rid of the empty prototype definitions.

3) create a dispatch table.
http://en.wikipedia.org/wiki/Dispatch_table
http://stackoverflow.com/questions/1281456/how-do-i-implement-dispatch-tables-in-perl

4) use an infinate while loop which will exit when the user selects option 8 (that's when you call the do_exit() sub via the dispatch table.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 36545554
Maybe I should give a brief reason as to why I made those suggestions.

This is obviously a class homework assignment and as it's written you will probably at best get a 'C' grade.  The suggestions I made introduce better quality coding standards and if done correctly will be much easier to read and maintain and you will receive a much higher grade.

There are a couple improvements that I left out and can point them out if you wish.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now