Solved

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

Posted on 2011-04-19
326 Views
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";
``````
0
Question by:Member_2_4213139

LVL 31

Expert Comment

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 ;
``````
0

LVL 31

Expert Comment

``````#!/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";
``````
0

LVL 31

Expert Comment

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

Author Comment

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

Author Comment

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";
``````
...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

LVL 31

Accepted Solution

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,
}
``````
0

LVL 31

Expert Comment

You were using \$Celsius without calculating it in the loop.  I just changed the order.  You need to calculate it before printing it.
0

LVL 31

Expert Comment

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

LVL 31

Expert Comment

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

Author Comment

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

Author Closing Comment

My code works PERFECTLY!  Thank you!!!
0

LVL 31

Expert Comment

I am glad it did.  You are most welcome.
0

## Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

## Featured Post

### Suggested Solutions

Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

#### 794 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!