Cormac Brady
Cormac Brady

Reputation: 95

Using a operator that is held in a string for a calulation

I am trying to make a program witch gives the user a random math question and answer but get errors if I try to store the operators in my array without them being in a string but this leads to problems when I want the program to solve the question as the operator is ignored and the two numbers added together. Please help!?!

    #!/usr/bin/perl

    use strict;
    use warnings;

    sub randomNumber  #generates a random number (1-100)
    {
        my $randomNumber = int(rand(100)) + 1;
        return "$randomNumber\n";
    }

    sub operators #randomly chooses a operator
    {
        my @list = ( "+" , "-" ,  "/"  , "*" );
        my $index = rand(@list);
        return $list[$index];
        }

    sub question{   
        my $firstNumber = randomNumber();
        my $operater = operators();
        my $secondNumber = randomNumber();
        print $firstNumber.$operater.$secondNumber."\n";
        print $firstNumber + $operater + $secondNumber;   #this is the problem line
    }

    question();

Here is a screenshot of the code when run:

thanks for the help here is my finished implementation for future readers:

#!/usr/bin/perl

use strict;
use warnings;

sub randomNumber
{
    my $randomNumber = int(rand(100)) + 1;
    return $randomNumber;
}

sub randomOperator
{
    my @operators = ( "+" , "-" ,  "/"  , "*" );
    my $index = rand(@operators);
    return $operators[$index];
    }

sub question{   
    my $firstNumber = randomNumber();
    my $operator = randomOperator();
    my $secondNumber = randomNumber();
    print "$firstNumber $operator $secondNumber =";
    my $userAnswer = <STDIN>;
    my $trueAnswer = eval "$firstNumber $operator $secondNumber";  #gets answer
    if ($userAnswer == $trueAnswer)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

print question(); 

Upvotes: 1

Views: 89

Answers (2)

ikegami
ikegami

Reputation: 385789

What's three plus asterisk? Yeah, that doesn't make much sense, but it's what you asked Perl to do

It looks like you're trying to generate and execute Perl code, but you'd use . instead of + instead of + to concatenate strings. And then you still have to pass the code to Perl to be compiled and executed, which can be done using eval EXPR.

my $code = '$firstNumber' . $operater . '$secondNumber';
my $result = eval($code);

Instead of generating Perl code, a more robust solution would be to use a dispatch table.

my %operations = (
   '*' => sub { $_[0] * $_[1] },
   '/' => sub { $_[0] / $_[1] },
   '+' => sub { $_[0] + $_[1] },
   '-' => sub { $_[0] - $_[1] },
);

my $result = $operations{$operater}->($firstNumber, $secondNumber);

Notes:

  • If you use the dispatch table, my @list = ...; becomes my @list = keys(%operations);.
  • $operater should be named $operator.
  • @list should be named @operators.
  • operators should be named randomOperator.

Upvotes: 4

mpapec
mpapec

Reputation: 50637

print eval "$firstNumber $operater $secondNumber";

instead of

print $firstNumber + $operater + $secondNumber;

Sanity checks before eval are also good idea when dealing with user input.

Upvotes: 2

Related Questions