Thanks again.

I've tried implementing the following code in Visual c++ 6:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <cmath>


int main(){

int binary, n;
double temp, total;

total =0;

cout << "Please enter the binary number: ";
cin >> binary;

n = floor(log10(binary)) + 1;

for(int i=1; i<=n; i++) {

temp = binary % (i * pow(10,i));

total = total + (temp * pow(2,i));
}

cout << "Decimal equivalent is: " << total;

return 0;
}

but I am getting the following error:


--------------------Configuration: 2_30a - Win32 Debug--------------------
Compiling...
2_30.cpp
C:\Documents and Settings\Hugh\My Documents\2_30a\2_30.cpp(24) : error C2297: '%' : illegal, right operand has type 'double'
Error executing cl.exe.

2_30.obj - 1 error(s), 0 warning(s)


How can I get round this should I convert reesult of power to type int?
or is there an overloaded version of % which works with type double?

You need to define suitable types or use type casting.

*** Furthermore, you should use code tags when you publish code on this forum.

> error C2297: '%' : illegal, right operand has type 'double'
the error message says it all, doesn't it?

> should I convert result of power to type int?
yes. either binary % ( i * int( pow(10,i) + 0.5 ) ) ; or binary % ( i * int( ceil( pow(10,i) ) ) ) ; note: this merely removes the compile time error. your program would now compile and run (but give incorrect results).

Honestly, I do not think your code work logically correct even if you had fixed the syntax error. I will demostrate how your code work in run-time:

Please enter the binary code: 100101
total = 0, n = 6, i = 1
temp = 100101 % (1 * 10) = 1
total = 0 + 1 * 2 = 2

i = 2
temp = 100101 % (2 * 100) = 101
total = 2 + 101 * 4 = 406
......


The proccess above has already proved that you have a logically error in your code. Moreover, temp and total are alway an integer, not double. There are alternative ways to achieve your purpose:

// I haven't test this code, but it should work fine.
int binary, sum = 0;

cout << "Please input the binary number:"
cin >> binary

do {
  sum += binary % 10;
  binary = binary / 10;
} while (binary);

cout << sum;

// I haven't test this code, ...

Then you shouldn't be posting it. There's nothing worse that helping someone with code that doesn't work. (well, yes there is, but you get the idea :-)

Comments
What ashame, I made 2 mistakes in the row in 1 hour :( feel really bad now

Then you shouldn't be posting it. There's nothing worse that helping someone with code that doesn't work. (well, yes there is, but you get the idea :-)

Ha ha, I make a small mistake at line 8. It should be sum = sum * 2 + (binary % 10); . So the finally draft is:

int binary, sum = 0;
cout << "Please input the binary number:"
cin >> binary
do {
      sum = sum * 2 + (binary % 10);
      binary = binary / 10;
} while (binary);
 cout << sum;

Now, I am sure it works now.

This article has been dead for over six months. Start a new discussion instead.