473,387 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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:

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
2 4450
RedSon
5,000 Expert 4TB
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
AjayGohil
83 64KB
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:
Expand|Select|Wrap|Line Numbers
  1. int binaryTodecimal()
  2. {
  3. //get binary number from user
  4.  
  5. //code for Converting binary to decimal
  6. }
  7. int decimalTobinary()
  8. {
  9.  
  10. //get decimal number from user
  11.  
  12. //code for Converting Decimal to binary
  13. }
  14. int main() {
  15.     int ch;  
  16.     cout <<"\nSelect Your Choice";
  17.     cout <<"1.Insertion Sort\n2.Bubble sort\n3.exit";
  18.     cout <<"enter ur choice:";
  19.      cin>>ch;
  20.   do
  21. {
  22.      switch (ch) {
  23.             case 1:
  24.                 binaryTodecimal()
  25.                 break;
  26.             case 2:
  27.                 decimalTobinary()
  28.                 break;
  29.             case 3:
  30.                 exit(0);
  31.             default:
  32.                 printf("Out of range");
  33.                 break;
  34.         }
  35. }while(ch!=3)
  36.      return 0;
  37.     }
Apr 23 '20 #3

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

Similar topics

1
by: GM | last post by:
Hello. I am starting my 7th week of an intro to programming class and have a problem I have been working on. My instructor said we could look for source code on the internet and use it as long as...
1
by: Brent W. Hughes | last post by:
I recently downloaded HelpMaker for free, but there was no documentation with it. Does someone know where I can find such documentation? Or, alternatively, where I can find another good free Help...
27
by: hpy_awad | last post by:
I wrote that program from a book that my compile that program correctly under C++ compiler but I got those errors for compiling under unix !! Errors- --------...
13
by: vgame64 | last post by:
Hi, I have been struggling with writing a program for a few hours. The requirements are that: """You will be writing a program which will determine whether a date is valid in terms of days in that...
9
by: k1ckthem1dget | last post by:
Can someone please help me with this program. How would i go about writing a program which determines the prime numbers from 2 to 1000 in increasing order using the “sieve method of Eratosthenes”....
28
by: Daleio | last post by:
hey im suppose to write a program using pelles C for windows to calculate the radius of the star using the formulas: r = 1/2T^2 *sqrt( L/pi*σ) L= 3.36192 *10^(140 - 2M/5) σ = 5.67051*10^ -8...
3
by: Daleio | last post by:
This is the question im suppose to answer, I don't really even know where to start............ Write a program to compute the partial sum of harmonic series 1 + 1/2 + 1/3 + L + 1/n and...
3
by: JJ297 | last post by:
Hello I'm a newbie to programming and need help writing an if statement. I have a database set up in SQL with the following fields: Category Questions Answers I only want one...
1
by: drandex | last post by:
Im stuck on one program. I am supposed to get the user's age, and what type of bus tickets they want. There are 3 age groups, and 3 ticket types. So for example of the User is a Senior, one bus...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.