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;

}

Recommended Answers

[QUOTE=1o0oBhP]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 ? would certainly simplify …

Jump to Post

All 8 Replies

Re: CAn any1 tell me watz wrong in da code 80 80

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.

Re: CAn any1 tell me watz wrong in da code 80 80

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;
}
Re: CAn any1 tell me watz wrong in da code 80 80

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.

Re: CAn any1 tell me watz wrong in da code 80 80

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

Re: CAn any1 tell me watz wrong in da code 80 80

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... :)

Re: CAn any1 tell me watz wrong in da code 80 80

ANd! To aDd to WhAT aLEx saId; pEAsE uSE prOPeR . punCtuATIon, ANd caPITaliSatION: IN yOuR quEstIonS?

Re: CAn any1 tell me watz wrong in da code 80 80

yes, that cost me about 5 minutes to deliberately get wrong ;)

Re: CAn any1 tell me watz wrong in da code 80 80

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--)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.