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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • star01daisy
    New Member
    • May 2010
    • 2

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

    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:

    Code:
    #include <iostream>
    #include <string>
    #include <math.h>
    using namespace std;
    
    int main()
    {
    
    // Conver a decimal number to binary
    
    int dec, len;
    string bin;
    
    // Ask user to enter a decimal numner
    
    cout << "Please enter a decimal number:" << endl;
    cin >> dec;
    bin = "";
    
    while (dec != 0)
    {
    if (dec % 2 == 0)
    bin.insert(0, "0");
    else
    bin.insert(0, "1");
    dec = dec / 2;
    }
    
    // Output decimal number to binary number
    
    cout << "The equivalent binary number is: " << bin << endl << endl;
    
    // Convert a binary number to decimal
    
    double deci;
    
    // Ask user to enter a binary number
    
    cout << "Enter a binary number:" << endl;
    cin >> bin;
    
    cout << bin << endl;
    
    len = (int) bin.length();
    
    cout << len << endl;
    deci = 0;
    for (int i=0; i<len; i++)
    if (bin.at(i) == '1')
    deci = deci + pow(2., len-i-1);
    
    // Output binary number to decimal number
    
    cout << "The equivalent decimal number is: " << deci << endl 
    << endl;
    }
    Last edited by RedSon; May 14 '10, 08:41 PM. Reason: Please be sure to use [CODE] tags in the future!
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    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:

    Code:
    if (userSelected bin to dec)
    {
        // Convert a decimal number to binary
    }
    else if (userSelected dec to bin)
    {
        // Convert a binary number to decimal
    }
    else
    {
        // output an error message saying the selection is invalid.
    }

    Comment

    • AjayGohil
      New Member
      • Apr 2019
      • 83

      #3
      Hi,

      You can create function for specific logic.Means you can create function that convert integer to binary and another function that convert binary to integer.and you can use switch case for option.This loop iterate until enter 3.

      Refer Following statement:
      Code:
      int binaryTodecimal()
      {
      //get binary number from user
      
      //code for Converting binary to decimal
      }
      int decimalTobinary()
      {
      
      //get decimal number from user
      
      //code for Converting Decimal to binary
      }
      int main() {
          int ch;  
          cout <<"\nSelect Your Choice";
          cout <<"1.Insertion Sort\n2.Bubble sort\n3.exit";
          cout <<"enter ur choice:";
           cin>>ch;
        do
      {
           switch (ch) {
                  case 1:
                      binaryTodecimal()
                      break;
                  case 2:
                      decimalTobinary()
                      break;
                  case 3:
                      exit(0);
                  default:
                      printf("Out of range");
                      break;
              }
      }while(ch!=3)
           return 0;
          }

      Comment

      Working...