Eri Mendz wrote:
> 
> Good day all,

Hello,

> Have here a temperature conversion program im trying to "perfect" but no
> chance, its not working right. The input validation portion is fine, im
> satisfied likewise with the conversion to celsius.  My problem is
> conversion to fahrenheit: the answer is not right so is the temp unit
> during printout. The problem must be obvious but i pick-up slow.  Can
> you help me out and guide me :-)
> 
> #!/usr/bin/perl -w
> use strict;
> 
> print "This is a temperature conversion program.\n";
> print "Enter temperature to convert(e.g., 100C, 212F): ";
> chomp(my $input = <STDIN>);
> if ($input =~ m/^([+-]?\d+)(\.{1}\d+)?[ ]?([cCfF])$/){  #validate input format
>     my $real_num = $1;
>     my $temp_unit = $3;

Should probably be:

if ($input =~ m/^([+-]?\d+(?:\.\d+)?)\s*([cCfF])$/){  #validate input
format
    my $real_num = $1;
    my $temp_unit = $2;


>     my ($in_c, $in_f) = ($real_num, $real_num);
>     if ($temp_unit eq 'C' or 'c'){

Should be:

      if ($temp_unit eq 'C' or $temp_unit eq 'c'){

Or better:

      if (lc $temp_unit eq 'c'){


>         $in_f = ($real_num * 9 / 5) + 32;
>         printf "%.2f C is %.2f F\n", $real_num, $in_f;
>     } else {    #it must be F if not C
>         $in_c = ($real_num - 32) * 5 / 9;
>         printf "%.2f C is %.2f F\n", $real_num, $in_c;

Should be:

          printf "%.2f F is %.2f C\n", $real_num, $in_c;


>     }
> } else {
>     #input failed validation check
>     print "Error is detected in input\n";
>     print "input format is number, optional space then letter C or F, case
>     insensitive, in exact order\n";
>     print "Please check your input and try again.\n";
> }



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to