Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How to make a perl file add random numbers that a user enters?

by rse2 (Initiate)
on Aug 03, 2011 at 18:26 UTC ( [id://918354]=perlquestion: print w/replies, xml ) Need Help??

rse2 has asked for the wisdom of the Perl Monks concerning the following question:

i have an assignment to make a perl file, where 1. ask the user to type in a number. 2. ask the user to type in another number. 3. ask user what is to be done multiply, add, subtract, divide. 4. display the correct answer.

So far i got the two first covered. but i am stumped at how to make it calculate random things. I am very new at this. Do i have to program to do every number or is there a way to make it calculate random things.

Thanks a million
  • Comment on How to make a perl file add random numbers that a user enters?

Replies are listed 'Best First'.
Re: How to make a perl file add random numbers that a user enters?
by toolic (Bishop) on Aug 03, 2011 at 18:55 UTC
    If you want to add 2 numbers, use +
    my $result = $x + $y;
    If you want to subtract 2 numbers, use -
    my $result = $x - $y;
    You could use an if/elsif/else block to decide what operator to use based on the user's input for step 3. See also perlsyn.
Re: How to make a perl file add random numbers that a user enters?
by dreadpiratepeter (Priest) on Aug 03, 2011 at 19:28 UTC

    I would suggest you post the code that you have so far, so we can offer suggestions. We can only guess at what your issue is from your vague description.

    Also, be aware that this site is neither a code writing service, nor a homework writing service. We will be happy to help you with problems if you put the work in, we will not do it for you.



    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: How to make a perl file add random numbers that a user enters?
by osbosb (Monk) on Aug 04, 2011 at 14:12 UTC
    You might need an if() block that handles your cases and you might need a while loop if you want to sanitize input... Here's some concept code:
    #!/usr/bin/perl use strict; use warnings; print "What would you like to do? <add|subtract|multiply|divide>: "; chomp(my $todo = <STDIN>); if($todo eq "add") { print "$first_num + $second_num = ", $first_num + $second_num, "\n +"; } elsif($todo eq "subtract") { print "$first_num - $second_num = ", $first_num - $second_num, "\n +"; } ...
    And so on.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://918354]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-19 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found