Binary to Decimal HELP :(

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 11
Reputation: egolovin is an unknown quantity at this point 
Solved Threads: 0
egolovin egolovin is offline Offline
Newbie Poster

Binary to Decimal HELP :(

 
0
  #1
Oct 10th, 2008
so i have spent 3 hours trying to figure out how exponents work in C++ but now. my if statement isnt working. even if i punch in a 0 it spits out 1
thanks in advance
  1. #include<iostream>
  2. #include<string>
  3. #include<cmath>
  4. using namespace std;
  5. void binToDec(string getBinary);
  6. void main()
  7. {
  8. string getBinary;
  9. cout<<"Enter a Binary Number:"<<endl;
  10. cin>>getBinary;
  11. binToDec(getBinary);
  12.  
  13.  
  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. cout<<total<<endl;
  32. }
  33.  
  34.  
  35.  
  36. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Binary to Decimal HELP :(

 
0
  #2
Oct 10th, 2008
1) Comparison of test with 1 has two equals signs, not 1 if (test == 1) . Having one equals sign assigns test to 1.

2) main() returns int, not void. Wash your mouth out with soap for returning void.
Last edited by grumpier; Oct 10th, 2008 at 6:42 pm. Reason: Fixed colouring!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 49
Reputation: dexter1984 is an unknown quantity at this point 
Solved Threads: 2
dexter1984 dexter1984 is offline Offline
Light Poster

Re: Binary to Decimal HELP :(

 
0
  #3
Oct 10th, 2008
Hmmm shouldn't
if (test=1)
be
if (test == 1)

Anyway I read that it isn't good to have void main().
Last edited by dexter1984; Oct 10th, 2008 at 6:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: egolovin is an unknown quantity at this point 
Solved Threads: 0
egolovin egolovin is offline Offline
Newbie Poster

Re: Binary to Decimal HELP :(

 
0
  #4
Oct 10th, 2008
haha im just making small programs for class thanks for the help. stupid mistake
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Binary to Decimal HELP :(

 
0
  #5
Oct 11th, 2008
It still won't work. You are comparing char with int:
getBinary[i] is char, and 1 is int.
char != int!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Binary to Decimal HELP :(

 
0
  #6
Oct 11th, 2008
Originally Posted by Sci@phy View Post
It still won't work. You are comparing char with int:
getBinary[i] is char, and 1 is int.
char != int!!!
It will work. Comparing char with int is allowed.

Worst case is a compiler warning (eg mismatch of precision of operands). A compiler warning means the programmer should check and make sure the code has the intended result, not that it is disallowed.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Binary to Decimal HELP :(

 
0
  #7
Oct 11th, 2008
This thread seems to be predecessor to http://www.daniweb.com/forums/thread150474.html
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Binary to Decimal HELP :(

 
0
  #8
Oct 12th, 2008
Originally Posted by grumpier View Post
It will work. Comparing char with int is allowed.

Worst case is a compiler warning (eg mismatch of precision of operands). A compiler warning means the programmer should check and make sure the code has the intended result, not that it is disallowed.
Yes, but does he want to compare test with '1' or with 1? Because that's different thing!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Binary to Decimal HELP :(

 
0
  #9
Oct 12th, 2008
Originally Posted by Sci@phy View Post
Yes, but does he want to compare test with '1' or with 1? Because that's different thing!
Ah, yes. Good point. Something for the original poster to contemplate .....
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC