Hi,
I'm having some trouble with my code that convert binary to decimal using recursion. this is my code
#include <cstdlib>
#include <iostream>
using namespace std;
void binaryToDecimal( int binaryNumber, int decimal, int weight );
int main(int argc, char *argv[])
{
// binary to decimal
int decimalNum;
int bitWeight;
int binaryNum;
decimalNum = 0;
bitWeight = 0;
cout <<"enter a number in binary: ";
cin >> binaryNum;
cout << endl;
binaryToDecimal(binaryNum, decimalNum, bitWeight);
cout << "Binary" << binaryNum << "=" << decimalNum
<<"decimal" << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
void binaryToDecimal( int binaryNumber, int decimal, int weight )
{
int bit;
if ( binaryNumber > 0 )
bit = binaryNumber % 10;
decimal = decimal + bit * pow(2, weight );
binaryNumber = binaryNumber / 10;
weight++;
binaryToDecimal(binaryNumber, decimal, weight );
}
my teacher said that i have to do the following errors, but I have no idea how to fix it..
1 - the binaryNumber variable should be a string, not an int, example: "110110"
2 - the function should return an int, the binary string converted to a decimal number
3 - your function never returns, i.e. never stops, that is the cause of the crash
Can someone please help me.
thanks,:)