• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

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

0
Member_2_4213139
Asked:
Member_2_4213139
  • 8
  • 4
1 Solution
 
farzanjCommented:
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

0
 
farzanjCommented:
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

0
 
farzanjCommented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Member_2_4213139Author Commented:
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!!!
0
 
Member_2_4213139Author Commented:
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?
0
 
farzanjCommented:
Is this what you wanted?

#!/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) {
        $Celsius = &convert($beginning);
        printf "\t%.0f\t\t%.0f\n", $beginning, $Celsius;
        $beginning = $beginning + $incValue,
}

Open in new window

0
 
farzanjCommented:
You were using $Celsius without calculating it in the loop.  I just changed the order.  You need to calculate it before printing it.
0
 
farzanjCommented:
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
0
 
farzanjCommented:
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.
0
 
Member_2_4213139Author Commented:
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...
0
 
Member_2_4213139Author Commented:
My code works PERFECTLY!  Thank you!!!
0
 
farzanjCommented:
I am glad it did.  You are most welcome.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now