Getting zero return value from Binary to decimal function

Guys I'm getting a 0 every time I run this program. I'm not sure if only the base case is getting executed or if I am just missing something. Can anyone tell me why anytime I run this my output is always 0?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  //Binary to Decimal

#include <iostream>
#include <cmath>
using namespace std;

int bin2dec(int num); //converts binary number to a decimal number
int bin2dec(int num , int pl); //overloaded function 

int bin2dec(int num)
{
	//call recursive function, point to rightmost digit
	return bin2dec(num,0);
}

int bin2dec(int num , int pl)
{
	if(num == 0)
		return 0;
	else
		return num % 10 * int(pow(2.0,pl)) * bin2dec(num/10 , pl+1);
}

int main()
{
	char again = 'Y';
	do{
		cout << "Enter a number in binary (1's and 0's) to see the decimal equivalent: ";
		int bin;
		cin >> bin;
		cout << "The decimal equivalent of " << bin << " is: " << bin2dec(bin) << endl;
		cout << "Another?(Y/N) : ";
		cin >> again;
	}while(toupper(again)=='Y');
}//endmain
You want to add the next digit along * bin2dec(num/10 , pl+1); should be * bin2dec(num/10 , pl+1);.
Thanks man I think I see where my error is
I can't believe I missed that thanks for pointing that out.
should be + bin2dec(num/10 , pl+1);
Last edited on
Topic archived. No new replies allowed.