whats wrong with my subroutines?

  • Hiei
  • Beginner
  • Beginner
  • Hiei
  • Posts: 51

Post 3+ Months Ago

i made a program and it works fine untill i put it an add subroutine, it displays the error
Code: [ Select ]
# syntax error, near ")
File 'Untitled #2'; Line 32
#
#
#
# sub add "
# syntax error, near "; }"
File 'Untitled #2'; Line 39
# Execution of Untitled #2 aborted due to compilation errors.
  1. # syntax error, near ")
  2. File 'Untitled #2'; Line 32
  3. #
  4. #
  5. #
  6. # sub add "
  7. # syntax error, near "; }"
  8. File 'Untitled #2'; Line 39
  9. # Execution of Untitled #2 aborted due to compilation errors.
when i take the sub routine with "add" out it works fine. anyone know what the problem is?

Code: [ Select ]
print "put in the name please:";

$login = <stdin>;
chomp ($login);


while ($login ne adam) {
print "put in the name please";
$login = <stdin>;
chomp ($login); }
print "what do you want to do";
$choice eq <stdin>;
chomp ($choice);
&divide if ($choice = divide);

sub divide {
print "this will divide any two numbers";

print "put in two numbers";

$divide = <stdin>;
$divide2 = <stdin>;

$divide3 = $divide/$divide2;

print "this is the answer";
print $divide3; }
&add if ($choice ne divide)



sub add { print "this will add any numbers";

$add = <stdin>;
$add2 = <stdin>;

$add3 = $add + $add2;

print $add3; print" is the answer"; }
  1. print "put in the name please:";
  2. $login = <stdin>;
  3. chomp ($login);
  4. while ($login ne adam) {
  5. print "put in the name please";
  6. $login = <stdin>;
  7. chomp ($login); }
  8. print "what do you want to do";
  9. $choice eq <stdin>;
  10. chomp ($choice);
  11. &divide if ($choice = divide);
  12. sub divide {
  13. print "this will divide any two numbers";
  14. print "put in two numbers";
  15. $divide = <stdin>;
  16. $divide2 = <stdin>;
  17. $divide3 = $divide/$divide2;
  18. print "this is the answer";
  19. print $divide3; }
  20. &add if ($choice ne divide)
  21. sub add { print "this will add any numbers";
  22. $add = <stdin>;
  23. $add2 = <stdin>;
  24. $add3 = $add + $add2;
  25. print $add3; print" is the answer"; }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

I moved some stuff around:
Code: [ Select ]
print "put in the name please:";
$login = <stdin>;
chomp ($login);

while ($login ne adam) {
    print "put in the name please";
    $login = <stdin>;
    chomp ($login);
}

print "what do you want to do";
$choice eq <stdin>;
chomp ($choice);

if ($choice = 'divide') { &divide;
} else { &add;    #instead of if($choice ne 'divide') because you just did that test in a positive manner
}

sub divide {
    print "this will divide any two numbers";
    print "put in two numbers";
    $divide = <stdin>;
    $divide2 = <stdin>;
    $divide3 = $divide/$divide2;
    print "this is the answer";
    print $divide3;
}

sub add {
    print "this will add any numbers";
    $add = <stdin>;
    $add2 = <stdin>;
    $add3 = $add + $add2;
    print "$add3 is the answer"; #compress this because there's no need for 2 print statements here.
}
  1. print "put in the name please:";
  2. $login = <stdin>;
  3. chomp ($login);
  4. while ($login ne adam) {
  5.     print "put in the name please";
  6.     $login = <stdin>;
  7.     chomp ($login);
  8. }
  9. print "what do you want to do";
  10. $choice eq <stdin>;
  11. chomp ($choice);
  12. if ($choice = 'divide') { &divide;
  13. } else { &add;    #instead of if($choice ne 'divide') because you just did that test in a positive manner
  14. }
  15. sub divide {
  16.     print "this will divide any two numbers";
  17.     print "put in two numbers";
  18.     $divide = <stdin>;
  19.     $divide2 = <stdin>;
  20.     $divide3 = $divide/$divide2;
  21.     print "this is the answer";
  22.     print $divide3;
  23. }
  24. sub add {
  25.     print "this will add any numbers";
  26.     $add = <stdin>;
  27.     $add2 = <stdin>;
  28.     $add3 = $add + $add2;
  29.     print "$add3 is the answer"; #compress this because there's no need for 2 print statements here.
  30. }


The real culprit for the error however, was probably the missing semicolon after the line that reads:
&add if ($choice ne divide)
but we eliminated that line anyway with our new if/else statement

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 151 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.