By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Help writing a C++ program to do decimal-binary number conversions.

P: 2
This is what the assignment says to do: Write a C++ program to do decimal-binary number conversions. The program gives the user a choice of conversion type (binary to decimal or decimal to binary). After the user makes a selection and enters an original datum to convert, the program should check if the input is valid. The user will be asked to reenter data until the input is acceptable. Finally, the original number and the converted form are both shown on the screen.
The main( ) function should be placed at the beginning of your program.


This is what i have but the teacher says i need to correct it, i need to programm it so the user chooses to do either the decimal to binary or binary to decimal and then display the results:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. // Conver a decimal number to binary
  10.  
  11. int dec, len;
  12. string bin;
  13.  
  14. // Ask user to enter a decimal numner
  15.  
  16. cout << "Please enter a decimal number:" << endl;
  17. cin >> dec;
  18. bin = "";
  19.  
  20. while (dec != 0)
  21. {
  22. if (dec % 2 == 0)
  23. bin.insert(0, "0");
  24. else
  25. bin.insert(0, "1");
  26. dec = dec / 2;
  27. }
  28.  
  29. // Output decimal number to binary number
  30.  
  31. cout << "The equivalent binary number is: " << bin << endl << endl;
  32.  
  33. // Convert a binary number to decimal
  34.  
  35. double deci;
  36.  
  37. // Ask user to enter a binary number
  38.  
  39. cout << "Enter a binary number:" << endl;
  40. cin >> bin;
  41.  
  42. cout << bin << endl;
  43.  
  44. len = (int) bin.length();
  45.  
  46. cout << len << endl;
  47. deci = 0;
  48. for (int i=0; i<len; i++)
  49. if (bin.at(i) == '1')
  50. deci = deci + pow(2., len-i-1);
  51.  
  52. // Output binary number to decimal number
  53.  
  54. cout << "The equivalent decimal number is: " << deci << endl 
  55. << endl;
  56. }
May 14 '10 #1
Share this Question
Share on Google+
1 Reply


RedSon
Expert 5K+
P: 5,000
You need to refactor your code. As the first item output a choice to the user to select bin to dec or dec to bin conversion. You should make your two conversion items into methods. Then you can do something like:

Expand|Select|Wrap|Line Numbers
  1. if (userSelected bin to dec)
  2. {
  3.     // Convert a decimal number to binary
  4. }
  5. else if (userSelected dec to bin)
  6. {
  7.     // Convert a binary number to decimal
  8. }
  9. else
  10. {
  11.     // output an error message saying the selection is invalid.
  12. }
  13.  
May 14 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.