CAn any1 tell me watz wrong in da code

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 12
Reputation: Extreme is an unknown quantity at this point 
Solved Threads: 0
Extreme Extreme is offline Offline
Newbie Poster

CAn any1 tell me watz wrong in da code

 
0
  #1
Dec 16th, 2004
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
>>>Extreme<<<
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: CAn any1 tell me watz wrong in da code

 
0
  #2
Dec 16th, 2004
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.
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 55
Reputation: r0ckbaer is an unknown quantity at this point 
Solved Threads: 6
r0ckbaer r0ckbaer is offline Offline
Junior Poster in Training

Re: CAn any1 tell me watz wrong in da code

 
0
  #3
Dec 16th, 2004
just a quick 'n' dirty hack here

  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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: CAn any1 tell me watz wrong in da code

 
0
  #4
Dec 16th, 2004
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.
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 55
Reputation: r0ckbaer is an unknown quantity at this point 
Solved Threads: 6
r0ckbaer r0ckbaer is offline Offline
Junior Poster in Training

Re: CAn any1 tell me watz wrong in da code

 
0
  #5
Dec 17th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: CAn any1 tell me watz wrong in da code

 
0
  #6
Dec 17th, 2004
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...
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: CAn any1 tell me watz wrong in da code

 
0
  #7
Dec 17th, 2004
ANd! To aDd to WhAT aLEx saId; pEAsE uSE prOPeR . punCtuATIon, ANd caPITaliSatION: IN yOuR quEstIonS?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: CAn any1 tell me watz wrong in da code

 
0
  #8
Dec 17th, 2004
yes, that cost me about 5 minutes to deliberately get wrong
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,141
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 947
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: CAn any1 tell me watz wrong in da code

 
0
  #9
Dec 18th, 2004
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--)
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2518 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC