943,813 Members | Top Members by Rank

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

Binary to Decimal HELP :(

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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. }
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 HELP :(

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!
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Oct 10th, 2008
0

Re: Binary to Decimal HELP :(

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.
Reputation Points: 31
Solved Threads: 2
Light Poster
dexter1984 is offline Offline
49 posts
since Jan 2008
Oct 10th, 2008
0

Re: Binary to Decimal HELP :(

haha im just making small programs for class thanks for the help. stupid mistake
Reputation Points: 10
Solved Threads: 0
Newbie Poster
egolovin is offline Offline
11 posts
since Oct 2008
Oct 11th, 2008
0

Re: Binary to Decimal HELP :(

It still won't work. You are comparing char with int:
getBinary[i] is char, and 1 is int.
char != int!!!
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 11th, 2008
0

Re: Binary to Decimal HELP :(

Click to Expand / Collapse  Quote originally posted by Sci@phy ...
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.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Oct 11th, 2008
0

Re: Binary to Decimal HELP :(

This thread seems to be predecessor to http://www.daniweb.com/forums/thread150474.html
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Oct 12th, 2008
0

Re: Binary to Decimal HELP :(

Click to Expand / Collapse  Quote originally posted by grumpier ...
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!
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 12th, 2008
0

Re: Binary to Decimal HELP :(

Click to Expand / Collapse  Quote originally posted by Sci@phy ...
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 .....
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 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: Scanf problem
Next Thread in C++ Forum Timeline: Bubble sorting and finding a location of a number in array





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


Follow us on Twitter


© 2011 DaniWeb® LLC