0

:?: converting binary numbers entered by the user to their equivalent decimal,program will have one function that takes an integer(1 and 0) upto 10 digits in length.Means binary into decimal ,I dont know where iam doing mistake, and my out put if i entered binary no 111 or any bin it gives 0 so plz help me where is the problem.
prototype function should look like this:
int binconvert (int binNum);
int main ()
{
int foo;
cout<<"enter number:";
cin>>foo;
cout<<biconvert(foo)<< ":"<<foo<<endl;
return0;
}
int binconvert (intNum)
{
long int n,i=1,d=0,m=0,l=0;
while(n>0)
{
m=(n%10);
n=(n/10);
l=m*i;
d=d+l;
i=i*2;
}
return d;
}

3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by Infarction
0

> int binconvert (intNum)
1. Give the parameter a type - say int
2. Actually use the parameter inside the function.

0

You might reconsider using an int as your parameter type. You're limited to 10 binary digits, which leaves you with binary values from 0 - 1023 decimal (at least on my system). If that's fine for your needs, then so be it; otherwise you might consider using a long or using a string and parsing each character as a '0' or '1'.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.