943,778 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 915
  • C++ RSS
Oct 10th, 2008
0

binary to decimal

Expand Post »
can someone look at this? in theory it should work but i cant figure it out
thanks in advance
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<string>
  3. #include<cmath>
  4. using namespace std;
  5. void binToDec(string getBinary);
  6. int main()
  7. {
  8. string getBinary;
  9. cout<<"Enter a Binary Number:"<<endl;
  10. cin>>getBinary;
  11. binToDec(getBinary);
  12.  
  13. return 0;
  14. }
  15. void binToDec(string getBinary)
  16. {
  17. int setLength,total=0;
  18. double placeholder=0;
  19. setLength=getBinary.size();
  20.  
  21. for(int i=setLength;i>0;i--)
  22. {
  23. int num=pow(2.0,placeholder);
  24. int test=getBinary[i];
  25. if (test==1)
  26. {
  27. total=total+num;
  28. }
  29.  
  30. placeholder=placeholder +1;
  31.  
  32. }
  33. cout<<total<<endl;
  34.  
  35.  
  36. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
egolovin is offline Offline
11 posts
since Oct 2008
Oct 10th, 2008
0

Re: binary to decimal

I guess using string is not the good idea.
What we use to do was.... some how take the binary in the int or long.

then take a loop and extract the element one by one form the last...

I guess this is the efficient method.. some how you are wasting too much memory space using string.

C++ Syntax (Toggle Plain Text)
  1. int dec;
  2. int bin;
  3. cin>>bin;
  4.  
  5. for(int i=0; bin!=0; bin/=10)
  6. dec+=bin%10*pow(2, i++);
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Oct 10th, 2008
0

Re: binary to decimal

oh sorry i forgot to add i have to use string as binary this is hw for school. pow only works with double.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
egolovin is offline Offline
11 posts
since Oct 2008
Oct 10th, 2008
0

Re: binary to decimal

Click to Expand / Collapse  Quote originally posted by egolovin ...
oh sorry i forgot to add i have to use string as binary this is hw for school. pow only works with double.

if you are such crazy about the string.
then you must subtract 48 from the each character of the string

as ASCII value of 1 is 49
when you subtract 49-48 = 1
then you can use it as normally
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Oct 10th, 2008
0

Re: binary to decimal

What
Click to Expand / Collapse  Quote originally posted by egolovin ...
can someone look at this? in theory it should work but i cant figure it out
thanks in advance
CPP Syntax (Toggle Plain Text)
  1. if (test==1)
That line should be if (test == '1')
1 is different to '1' (character code 49, as Rhohitman said). Also, for powers of 2 you don't have to use pow(). Using the shift operator<< works fine for integers. So pow(2., x) can be replaced with 1<<x
Last edited by dougy83; Oct 10th, 2008 at 10:52 pm.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Oct 11th, 2008
0

Re: binary to decimal

Thanks alot :d
Reputation Points: 10
Solved Threads: 0
Newbie Poster
egolovin is offline Offline
11 posts
since Oct 2008

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: Simple manipulation help needed.
Next Thread in C++ Forum Timeline: RichEdit subclassing problem, can't call Default Proc.





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


Follow us on Twitter


© 2011 DaniWeb® LLC