Hi al...I am a total c++ beginner...I need ur help plz...I am making a program which asks the user to input a binary number and displayz the binary number converted to decimal number using a function.

The code which i wrote is below:

#include<iostream.h>

#include<math.h>

int bin2dec(int n)

{

int k=0, sum=0,x;

while(n>0)

{
x=n%10;

sum+=x*pow(2,k++);

n/=10;

}

return sum;

}

void main()

{

int n,dec;

cout<<"Enter any binary number"<<endl;

cin>>n;

cout<<"The binary number converted to decimalform= "<<dec=bin2dec(n);

cout<<endl;

}

Can you do us all a favor? Plz don't uz AOLspk in hr. U dont need 2 uz cute wurdz here.

If you're asking for help in a forum, the absolute best thing you can do is be as clear as possible, which means stop speaking like you're in some lol d00d chat room. Doing anything else will detract from your potential to recieve help.

just a quick 'n' dirty hack here :P

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

int Bin2dec(string strBin)
{
	int nSum = 0, k = strBin.length() - 1;

	for (int i = 0; i < strBin.length(); i++)
		nSum += (strBin[i] - '0') * pow(2, k--);

	return nSum;
}

int main(void)
{
	string strBin;

	
	cout << "Enter binary number :";
	cin >> strBin;

	cout << "Decimal value: " << Bin2dec(strBin) << endl;

	return 0;
}

Theres another thread going with the same problem. I have posted C++ code and someone has also posted a C version of a binary to decimal program. The post is "I need binary to decimal code" or sumthin like that on the c/c++ main board.

That's not important, he wanted to have his thinggy corrected, and that's what i did...this answer was specifically addressed to his problem

That's not important, he wanted to have his thinggy corrected, and that's what i did...this answer was specifically addressed to his problem

not having a go, dont worry! just saying that if there is anything that cant be sorted then a few people have discussed the topic of going both ways (b2d, d2b) on a different thread. btw where is the pow function defined ? is it in <cmath>? would certainly simplify my own code version... :)

not having a go, dont worry! just saying that if there is anything that cant be sorted then a few people have discussed the topic of going both ways (b2d, d2b) on a different thread. btw where is the pow function defined ? is it in <cmath>? would certainly simplify my own code version... :)

Yes, pow() is in cmath, but likes to see doubles for arguments, or at least pow(2.0, k--)

This question has already been answered. Start a new discussion instead.