I have the question in Perl:
To write a Perl script which will ask the user for a temperature and then ask whether it is to be converted to degree Celius or Fahrenheit. Perform the conversion and display the answer. The equations for temperature conversion are:
1) Celsius to Fahrenheit:C=(F-32) x 5/9
2) Fahrenheit to Celsius:F=9C/5 + 32
My script is:
#!/usr/bin/perl
use strict;
use warnings;
print "Enter the temperature: ";
my $temp = <STDIN>;
print "Enter the Conversion to be performed:";
my $conv = <STDIN>;
my $cel;
my $fah;
if ($conv eq 'F-C') {
$cel = ($temp - 32) * 5/9;
print "Temperature from $fah degree Fahrenheit is $cel degree Celsius";
}
if ($conv eq 'C-F') {
$fah = (9 * $temp/5) + 32;
print "Temperature from $cel degree Celsius is $fah degree Fahrenheit";
}
After I enter $temp and $conv from the keyboard, blank output will appear.Where am I going wrong? Please help. Thanks in advance.
After the input, you will have a newline character in your variables. Use
chompto get rid of it.Then there will be a second problem – you are using
$fahor$celin your output statement. This should be the$tempvariable, otherwise you will get an error like this:Here is the updated code: