May anyone tell me if this program is wrong or if there is a
implementation problem in perl 5.8.6, 5.8.5, 5.8.2
I will show the perl version and a c++ version. The c++ version
worked. I will also show the version of the perl version that
worked with a number changed.
The correct version of perl:
#!/usr/bin/perl
# Input an integer containing only 0s and 1s and print its decimal
equivalent
$counter = 1;
$counter2 = 1;
$total = 0;
print "Enter a binary number\n";
$number = <STDIN>;
chomp $number;
while( ($number / $counter) != 0 )
{
$number1 = ($number / $counter) % 10;
$total = $total + ($number1 * $counter2);
$counter = $counter * 10;
$counter2 = $counter2 * 2;
}
print "The decimal equivalent of ", $number, " is ", $total, "\n";
perl wrong version:
#!/usr/bin/perl
# Input an integer containing only 0s and 1s and print its decimal
equivalent
$counter = 1;
$counter2 = 1;
$total = 0;
print "Enter a binary number\n";
$number = <STDIN>;
chomp $number;
while( ($number / $counter) != 0 )
{
$number1 = ($number / $counter) % 10;
$total = $total + ($number1 * $counter2);
$counter = $counter * 10;
$counter2 = $counter2 * 16; // changed 2 to 16
}
print "The decimal equivalent of ", $number, " is ", $total, "\n";
The c++ version of the wrong perl version:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int counter = 1;
int counter2 = 1;
long int total = 0;
int number;
int number1;
cout << "Enter a binary number" << endl;
cin >> number;
while( (number / counter) != 0 )
{
number1 = (number / counter) % 10;
total = total + (number1 * counter2);
counter = counter * 10;
counter2 = counter2 * 16;
}
cout << "The decimal equivalent of " << number << " is " <<
total << endl;
return 0;
}
_______________________________________________
Perl-Unix-Users mailing list
Perl-Unix-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs