I have to convert a decimal number (1-255) to packed binary-coded decimal.
I have the idea of how it's supposed to work.
Example:
Convert 133 to BCD
1. Separate 133 to individual numbers (1, 3, 3).
2. Convert each number to binary (0001, 0011, 0011).
3. Print it in the form of "133 - 0001 0011 0011".
However, I'm struggling how to get the number separation and whatnot.
I'm just really confused how to program this.
Also, the decimal numbers have to be in 3 values, (eg; 4 has to be 004)
Because the result to BCD should be 0000 0000 0100, rather than just 0100.
If someone can help me with some C++ algorithm or snippet of code with some explanation, I would really appreciate it.
Thank you Duoas again, however, I still have a problem with the option you provided.
Upon the best estimated location of putting these 2 line of codes, I was only able to get a result like this:
I didn't learn much in strings, so I may need help with that.
I'm just trying to convert the binaryConv function to string.
I'm just struggling with a bunch of errors like "no operator "<=" matches these operands", so on so forth. Can you point me to the right direction using strings?
Instead of cout << num; use s += (char)(num + '0'); or s += (char)(remain + '0');
Is there a reason your function is recursive? You might find it easier to do if it isn't... But if you do keep it recursive, you'll have to build your string recursively also. For example, here is something to play with:
Oh wow! Thanks for the reply Duoas, but this is beyond my knowledge at the moment.
Can you explain what's going on in this? s += (char)(num + '0'); and as well as in the main code. Very unfamiliar with strings, I just need some explanation on what stuff like the getline is doing, what is istringstream ss (s), same with "unsigned". I'm just entirely confused.
I hate to make you do much more, but I need to figure out what everything does, then I can make some changes, like getting rid of recursion (like you partially recommended), as well as adding a for loop (needs to display 1-255) instead of the while.
I can try make some assumptions myself; while (getline( cin, s ) && !s.empty())
- Does this mean it gets the information from what the user typed and gets defined as string s?