in reply to String/Numeric Manipulation
The while test on the value of days is made after the conversion to seconds is done so when you enter q you are trying to convert it to seconds. you could do a few things, here is one that keeps a while loop...
#!/usr/local/bin/perl -w use strict; while (1) { print "Please enter a single positive integer that represents a number of da +ys, that you wish to convert into seconds. ,(enter q to quit): "; chomp (my $days = <STDIN>); $days =~ s/\+//; # allow them to enter +12 if ($days eq "q") { last; } elsif (($days =~ /[^\d]/) or ($days eq "")) { # days contains an non digit character or is empty print "\n!!! You must enter a positive integer.\n\n"; } else { my $seconds = $days * 86400; my $plural = ($days == 1) ? '' : 's'; print "\n$days day$plural = $seconds seconds.\n\n"; } } print "Goodbye\!\n\n";
update to catch entry of 0
Change the elsif check to this if you want to prevent them from entering zero days} elsif (($days =~ /[^\d]/) or (not $days)) {
Cheers,
R.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: String/Numeric Manipulation
by Elgon (Curate) on Oct 07, 2004 at 18:56 UTC | |
by Random_Walk (Prior) on Oct 08, 2004 at 12:05 UTC |
In Section
Seekers of Perl Wisdom