954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

CAn any1 tell me watz wrong in da code

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

#include

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"<>n;

cout<<"The binary number converted to decimalform= "<

Extreme
Newbie Poster
12 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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.

alc6379
Cookie... That's it
Team Colleague
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
 

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;
}
r0ckbaer
Junior Poster in Training
55 posts since Dec 2003
Reputation Points: 13
Solved Threads: 6
 

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.

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

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

r0ckbaer
Junior Poster in Training
55 posts since Dec 2003
Reputation Points: 13
Solved Threads: 6
 
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 ? would certainly simplify my own code version... :)

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

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

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
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 my own code version... :)


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

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You