Rusty S Rusty S - 1 year ago 60
Perl Question

perl - Trying to use a while loop to ask the user if they want to do that again

(New to perl)
I have a small perl program that calculates factorials. I'd like to use a while loop so that after the user gets a result, they will be asked "Calculate another factorial? Y/N" and have Y run the code again & have N end the program.

Here's my code:

print"Welcome! Would you like to calculate a factorial? Y/N\n";

$decision = <STDIN>;

while $decision == "Y";
print"Enter a positive # more than 0: \n";

$num = <STDIN>;
$fact = 1;

$fact = $fact * $num;
$num $num - 1;

print $fact\n;
print"Calculate another factorial? Y/N\n";
$decision = <STDIN>;

What's giving me trouble is where to put the while loop and how to make the Y/N option work. I'm also unclear about
functions. I do know that
makes my programs work though.

Answer Source

Your program is almost right, just a few issues:

  1. Please get used to always add use strict; and use warnings; to your scripts. They will (beyond other things) force you to declare all the variables you use (with my $num=…;) and warn you about common errors (like typos). Some people consider it a bug that use strict; and use warnings; aren't turned on by default.
  2. When reading a line from STDIN (or some other filehandle) the read line will contain the trailing newline character "\n". For your comparison to work you must get rid of that using the chomp function.
  3. There are two different sets of comparison operators in Perl: one for strings and one for numbers. Numbers are compared with <, >, <=, >=, ==, and !=. For strings you must use lt (less-than), gt, le (less-or-equal), ge, eq, and ne. If you use one of the number operators on strings Perl will try to interpret your string as a number, so $decision == "Y" would check whether $decision is 0. If you had use warnings; Perl would have noticed you. Use $decision eq "Y" instead.
  4. The outer while loop had a trailing ; just after the comparison which will give you an endless loop or a no-op (depending on the content of $decision).
  5. You forgot the = in $num = $num - 1;.
  6. You forgot the quotes " around print "$fact\n";
  7. system("pause") only works on Windows where pause is an external command. On Linux (where I just tested) there is no such command and system("pause") fails with command not found. I replaced it with sleep(5); which simply waits 5 seconds.


#!/usr/bin/env perl

use strict;
use warnings;

print "Welcome! Would you like to calculate a factorial? Y/N\n";

my $decision = <STDIN>;
chomp($decision);    # remove trailing "\n" from $decision

while ( $decision eq 'Y' ) {
    print "Enter a positive # more than 0: \n";

    my $num = <STDIN>;
    my $fact = 1;

    while ( $num > 1 ) {
        $fact = $fact * $num;
        $num  = $num - 1;

    print "$fact\n";
    print "Calculate another factorial? Y/N\n";
    $decision = <STDIN>;
print "ok.\n";

sleep(5);    # wait 5 seconds
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download