943,972 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2742
  • C++ RSS
Dec 16th, 2004
0

CAn any1 tell me watz wrong in da code

Expand Post »
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;

}
Last edited by Extreme; Dec 16th, 2004 at 5:19 pm. Reason: Small mistake
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Extreme is offline Offline
12 posts
since Nov 2004
Dec 16th, 2004
0

Re: CAn any1 tell me watz wrong in da code

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.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Dec 16th, 2004
0

Re: CAn any1 tell me watz wrong in da code

just a quick 'n' dirty hack here

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. int Bin2dec(string strBin)
  8. {
  9. int nSum = 0, k = strBin.length() - 1;
  10.  
  11. for (int i = 0; i < strBin.length(); i++)
  12. nSum += (strBin[i] - '0') * pow(2, k--);
  13.  
  14. return nSum;
  15. }
  16.  
  17. int main(void)
  18. {
  19. string strBin;
  20.  
  21.  
  22. cout << "Enter binary number :";
  23. cin >> strBin;
  24.  
  25. cout << "Decimal value: " << Bin2dec(strBin) << endl;
  26.  
  27. return 0;
  28. }
Reputation Points: 13
Solved Threads: 6
Junior Poster in Training
r0ckbaer is offline Offline
55 posts
since Dec 2003
Dec 16th, 2004
0

Re: CAn any1 tell me watz wrong in da code

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.
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 17th, 2004
0

Re: CAn any1 tell me watz wrong in da code

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
Reputation Points: 13
Solved Threads: 6
Junior Poster in Training
r0ckbaer is offline Offline
55 posts
since Dec 2003
Dec 17th, 2004
0

Re: CAn any1 tell me watz wrong in da code

Quote ...
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...
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 17th, 2004
0

Re: CAn any1 tell me watz wrong in da code

ANd! To aDd to WhAT aLEx saId; pEAsE uSE prOPeR . punCtuATIon, ANd caPITaliSatION: IN yOuR quEstIonS?
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 17th, 2004
0

Re: CAn any1 tell me watz wrong in da code

yes, that cost me about 5 minutes to deliberately get wrong
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 18th, 2004
0

Re: CAn any1 tell me watz wrong in da code

Quote originally posted by 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 <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--)
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: HELP! Illegal Character Constant in a Console Application
Next Thread in C++ Forum Timeline: garbage value ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC