in reply to Simple Arithmetic Question

OK, just to get you started, this one possible way (untested, but hopefully right) to do the beginning:
use strict; use warnings; print "Please enter three numbers separated by spaces (complete by hit +ting the enter key)\n"; my $user_input = <>; # get user input chomp $user_input; # removing trailing newline from us +er input my ($nr1, $nr2, $nr3) = split /\s+/, $user_input; # splitting + user input on spaces to get the 3 numbers print "The sum of the three numbers is equal to ", ($nr1 + $nr2 + $nr3 +), "\n"; print "The product ...
This is not really the way I would personally write such a program, but I tried to make it quite simple for a beginner like you to understand it. Hopefully you should be able to complete by yourself.

I asked the user to enter the three numbers on the same line because I was too lazy to ask 3 times for a number and and get the user input three times in a row, but if you haven't covered yet the split function, you might as well change it to three questions, three standard input captures and three chomp's, once for each of the 3 variables.

I also changed the name of your variables because $a and $b are special variables (used for sorting data) that it would be better not to use for other purposes, although this has probably no importance in the case of your assignment (but it would be better not to get into the habit of using them for something else). But, of course, if your instructor is asking you to use $a, $a and $c, do it in accordance to his or her wishes.

One additional advise: when you'll try to code the division ($a / $c) and the remainder of the division ($a % $b), check before that $c and $b are not zero (to avoid a division by 0 error).

Replies are listed 'Best First'.
Re^2: Simple Arithmetic Question
by pcoady (Novice) on Nov 06, 2014 at 03:48 UTC

    This is what I have so far I'm not sure how to use the chomp exactly so I'm sure that is wrong. It keeps showing a Global symbol "c" requires explicit package name in lines 14 and 17. I'm sure there are probably multiple errors in the code but I can't figure it out. If you have any pointers that would be great thanks!!

    #!/usr/bin/perl use Modern::Perl; use strict; use warnings; print "input three numbers separated by spaces\n"; my $user_input = <>; chomp $user_input; my ($nr1) = $user_input, "$a"; chomp $user_input; my ($nr2) = $user_input, "$b"; chomp $user_input; my ($nr3) = $user_input, "$c"; print "The sum of the three numbers is equal to", ($a+$b+$c),"\n";
      print "input three numbers separated by spaces\n"; my $user_input = <>;

      This is good as far as it goes: you're getting something | a string from  STDIN that is terminated by a newline.

      chomp $user_input;

      This is good too, except you do it twice more and this is where you start to go off the rails. chomp removes a newline (if present) from the end of a string (well, it's a little more involved than that, but let it stand for now), $user_input in this case, so doing it repeatedly will not usually get you anything. For learning/development/debugging, I would advise placing a print statement after this step so you can be sure exactly what you are dealing with:
          print "A: '$user_unput' \n";
      (In general, develop your code step-by-step and use print statements at each successive step to see just what is happening. If weird stuff starts to happen, understand the problem before proceeding.)

      my ($nr1) = $user_input, "$a";

      This has no meaning. You are assigning  $user_input to  $nr1 (why?) and interpolating  $a (which has no value at this point) into a string, which you then throw away: what do you think is happening here? (Update: Two other, similar statements have the same problem.)

      Please consider these and other points raised so far, write some more code and post again.

      Update: Do continue to  use strict; and  use warnings; in your code (use Modern::Perl; includes both of these). They will make learning Perl much easier.

      Either you get all 3 numbers separated by spaces on one line, chomp it once and then you have to use split or some other mechanism to get the individual numbers (the way I did it in my previous post), or you ask three times for one indivudal number from your user, get three times the input, chomp the input three times.
      print "give me a number\n"; my $nr1 = <>; chomp $nr1; print "give me a number\n"; my $nr2 = <>; chomp $nr2; print "give me a number\n"; my $nr3 ...