Convert Binary to Decimal and from Decimal to Binary

Thread Solved
Reply

Join Date: Nov 2008
Posts: 3
Reputation: Gamergirl22 is an unknown quantity at this point 
Solved Threads: 0
Gamergirl22 Gamergirl22 is offline Offline
Newbie Poster

Convert Binary to Decimal and from Decimal to Binary

 
0
  #1
Nov 20th, 2008
Hi, I have a program problem with converting decimals to binary and back again. My teacher said that the program is to be like a menu and convert decimals or binary from the user's input. This is part of my code:
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. void binary(int number) {
  7. int remainder;
  8. if(number <= 1)
  9. cout << number;
  10. remainder = number % 2;
  11. binary(number >> 1);
  12. cout << remainder;
  13. }
  14.  
  15. int main(){
  16.  
  17. int number;
  18. int choice;
  19.  
  20. cout << "1. Binary to Decimal\n";
  21. cout << "2. Decimal to Binary\n";
  22. cout << "3. Quit Program\n";
  23. cout << "Enter your Choice: ";
  24. cin >> choice;
  25.  
  26. switch (choice) {
  27.  
  28. case '1':
  29. cout << "Please enter an integer: ";
  30. cin >> number;
  31. if (number < 0)
  32. cout << "Error enter positive integer: ";
  33. else {
  34. cout << "The number in Binary is: ";
  35. binary(number);
  36. cout << endl;
  37. }
  38.  
  39.  
  40. }
  41. system("pause");
  42. return 0;
  43. }
I know my problem is with the binary(int number) function because it is runnable but dosen't call to the function. So, my question is how can I modify this to run correctly? Also, how would I go about reversing the code to have it convert binary to decimal?
Last edited by Narue; Nov 20th, 2008 at 6:51 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: Convert Binary to Decimal and from Decimal to Binary

 
0
  #2
Nov 20th, 2008
Nice thought to use the recursive function...

recursive function should have the terminating point which in the conversion always turn to 1 at last.

  1. void binary(int num)
  2. {
  3. if(num!=1)
  4. {
  5. int rem=num%2;
  6. binary(num/2);
  7. cout<<rem;
  8. }
  9. else
  10. cout<<num;
  11. }
Last edited by Rhohitman; Nov 20th, 2008 at 6:59 pm.
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 3
Reputation: Gamergirl22 is an unknown quantity at this point 
Solved Threads: 0
Gamergirl22 Gamergirl22 is offline Offline
Newbie Poster

Re: Convert Binary to Decimal and from Decimal to Binary

 
0
  #3
Nov 20th, 2008
Thanks Rohitman. So, How do I reverse it as in binary to decimal? I know it has something to do with diffrent powers of a number like 2x^i. So, do I use a for loop for that or something else?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 89
Reputation: brechtjah is an unknown quantity at this point 
Solved Threads: 9
brechtjah's Avatar
brechtjah brechtjah is offline Offline
Junior Poster in Training

Re: Convert Binary to Decimal and from Decimal to Binary

 
0
  #4
Nov 20th, 2008
1 = 1
10 = 2
100 = 4
1000 = 8

You can solve this in more than one way, well I can think of multiple ways atleast ^^
Hope this helps
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: Convert Binary to Decimal and from Decimal to Binary

 
0
  #5
Nov 20th, 2008
Originally Posted by Gamergirl22 View Post
Thanks Rohitman. So, How do I reverse it as in binary to decimal? I know it has something to do with diffrent powers of a number like 2x^i. So, do I use a for loop for that or something else?
yes, you have to do some thing like that only. It your wish to use the recursive function or not but I don't recommend to use the recursive function in this case and earlier one too.

In any number system is represented by the general expression
a[n]*r^[n] + a[n-1]*r^[n-1] +........+ a[2]*r^[2] + a[1]*r^[1]+a[0] //integer part
+ a[-1]*r^[-1] + + a[-1]*r^[-2].......+ a[-m]*r^[-m] //fraction part

where,
a[j] is the coefficient
r is radix or base

For decimal no system r = 10; and coefficient can have the value from 0-9

Similarly the binary no system base will be 2, and can have only 2 values 0 and 1.

I guess concept would be clear...
best of luck for programming..
Last edited by Rhohitman; Nov 20th, 2008 at 9:50 pm.
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 3
Reputation: Gamergirl22 is an unknown quantity at this point 
Solved Threads: 0
Gamergirl22 Gamergirl22 is offline Offline
Newbie Poster

Re: Convert Binary to Decimal and from Decimal to Binary

 
0
  #6
Nov 21st, 2008
Thanks, I think I got it. I finally got additional help from one of my friends in class.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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