Member_2_4213139
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";
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";
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.
printf "%.0f", $val;
You can also use Math::Round
If you have that class.
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!!!
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!!!
ASKER
OK... I changed my code to this:
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
ASKER
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...
ASKER
My code works PERFECTLY! Thank you!!!
I am glad it did. You are most welcome.
Open in new window