Link to home
Start Free TrialLog in
Avatar of Member_2_4213139
Member_2_4213139Flag for United States of America

asked on

How do I get the first number in Celsius to print and rounded to a whole number?

How do I get the first number in Celsius to print and rounded to a whole number?

#!/usr/bin/perl
print "Content-type: text/html\n\n";

sub convert {
   my $fahr = $_[0];
   my $cels = ($fahr - 32) * 5/9;
   return $cels}

$absolute = -459.69;
print "Temperature Program\n\n";
print "What is the Beginning Fahrenheit Temperature: ";
chomp($beginning = <STDIN>);
print "\n";
print "What is the Ending Fahrenheit Temperature: ";
chomp($ending = <>);
print "\n";
print "What is the Increment Value: ";
chomp($incValue = <>);
print "\n\n";

if ($ending < $beginning) {
print STDERR "Beginning Temperature must be lower than than Ending Temperature $
if ($beginning < $absolute or $ending < $absolute) {
print STDERR "Temperatures cannot go below absolute zero \n";}

print "\tFahrenheit\tCelsius\n\n";
while ($beginning <= $ending) {
        print "\t$beginning\t\t$Celsius\n";
        $beginning = $beginning + $incValue,
        $Celsius = &convert($beginning);}
print "\n\n";

Open in new window

Avatar of farzanj
farzanj
Flag of Canada image

Here is some code to help you.
#! /usr/bin/perl

use strict;
use warnings;

sub toCelcius
{
        return ($_[0] - 32)*5/9;
}
print "Enter Farenheit value : ";
my $f = <STDIN>;
my $c = toCelcius($f);
print "Celcius is : $c \n";
printf  "Rounded off %.0f", $c ;

Open in new window

I made your code work too.


#!/usr/bin/perl
print "Content-type: text/html\n\n";

sub convert {
   my $fahr = $_[0];
   my $cels = ($fahr - 32) * 5/9;
   return $cels}

$absolute = -459.69;
print "Temperature Program\n\n";
print "What is the Beginning Fahrenheit Temperature: ";
chomp($beginning = <STDIN>);
print "\n";
print "What is the Ending Fahrenheit Temperature: ";
chomp($ending = <>);
print "\n";
print "What is the Increment Value: ";
chomp($incValue = <>);
print "\n\n";

if ($ending < $beginning) {
die  "Beginning Temperature must be lower than than Ending Temperature ";
}
if ($beginning < $absolute or $ending < $absolute) {
die "Temperatures cannot go below absolute zero \n";
}

print "\tFahrenheit\tCelsius\n\n";
while ($beginning <= $ending) {
        print "\t$beginning\t\t$Celsius\n";
        $beginning = $beginning + $incValue,
        $Celsius = &convert($beginning);}
print "\n\n";

Open in new window

As you can see in my code, you can round it off by simply using the printf command

printf "%.0f", $val;


You can also use Math::Round

If you have that class.
Avatar of Member_2_4213139

ASKER

farzanj,

The code you posted (that you said you fixed) is my original code... And it still fails to print the first Celsius value AND I couldn't get your other suggestions to work in the code to round the Celsius numbers, either!

PLEASE let me know what I'm doing wrong!  THANK YOU!!!
OK... I changed my code to this:
print "\tFahrenheit\tCelsius\n\n";
while ($beginning <= $ending) {
        printf "\t%.0f\t\t%.0f\n", $beginning, $Celsius;
        $beginning = $beginning + $incValue,
        $Celsius = &convert($beginning);}
print "\n\n";

Open in new window

...and it works beautifully!

HOWEVER, the first value in the list prints a "0" ...

Sample Output:

Temperature Program

What is the Beginning Fahrenheit Temperature: 200

What is the Ending Fahrenheit Temperature: 215

What is the Increment Value: 3


        Fahrenheit      Celsius

        200             0
        203             95
        206             97
        209             98
        212             100
        215             102

How is this fixed?
ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You were using $Celsius without calculating it in the loop.  I just changed the order.  You need to calculate it before printing it.
Here's the output


Temperature Program

What is the Beginning Fahrenheit Temperature: 200

What is the Ending Fahrenheit Temperature: 215

What is the Increment Value: 3


      Fahrenheit      Celsius

      200            93
      203            95
      206            97
      209            98
      212            100
      215            102
When I first got you code, it was not compiling.  I had to use die instead of STDERR.  There were a few more syntax errors that I had to get rid of.
You know, I moved that line ALL AROUND and it didn't do ANYTHING... now I move it, and it fixed the problem!  THANK YOU SO MUCH!!!  I bow to your expertise...
My code works PERFECTLY!  Thank you!!!
I am glad it did.  You are most welcome.