My professor gave us an assignment to Convert the following 2 numbers to binary, octal and hex. 3.567, 0.032432
which would be easy if the numbers given weren't in decimals. I'm using the % operator and that can't be used by float... I'm lost. Can someone give me an idea what to do?
heres one part of my code for the binary conversion.

``````#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int num;
int total = 0;
cout << "Please enter a decimal: ";
cin >> num;
while(num > 0)
{
num /= 2;
total = num % 2;

cout << total << " ";
}
return 0;
}``````

Good that you tried.
I suggest, write a function separately for each, binary, octal and hex.
How to?
1. Write a set of steps to convert a decimal to binary, in pure mathematical way.
2. Now try to write code in a function that maps to your steps. Pass your …

## All 8 Replies

Good that you tried.
I suggest, write a function separately for each, binary, octal and hex.
How to?
1. Write a set of steps to convert a decimal to binary, in pure mathematical way.
2. Now try to write code in a function that maps to your steps. Pass your decimal to that function as argument.

Do follow similar procedure for other conversions. Best Luck.

Thats what I was thinking about doing but I'm not sure how to go about doing that with decimals...
with binary for example. if I have a number like 2.125, how would I get the binary for that, even with math? The decimal at the end messes me up and I have no idea how to get the numbers past the decimal...

Ah I see now! I understand the formula...
My idea was to seperate the conversion into two different parts. The first part is the left side of the decimal which I already posted up. and for the second part/ the right side of the decimal, I was going to do it straight mathematically.

so this is what I could come up with for now. I tried to create a structure so that when the variable is entered, it would just loop through the program till 1 is reached and it would end...

``````#include<iostream>
#include<string>
#include<math.h>
using namespace std;
struct the
{
float numbers;

};
int main() //Start of main function
{

the num;

cout <<" enter decimal " <<endl;
cin >> num.numbers;
do
{
num.numbers=num.numbers*2;
if (num.numbers>1)
{
num.numbers=num.numbers-1;
cout <<"1"<<endl;
}
if (num.numbers<1)
{

cout<<"0"<<endl;
}
}
while (num.numbers!=1);
return 0;
}``````

if i were to write a solution, i would roughly design my application as below.

``````void ReadInput(int *pNum);
void ConvertToBinary(int *pNum);
void ConvertToHex(int *pNum);
void ConvertToOctal(int *pNum);``````

This would be better as it segregates the logic.

yea I was planning on using functions to make things more organized, but I'm still trying to figure out how to convert numbers to binary without using Modulus.
can someone give me a short example on how to go about doing this?

hi there..
i have gathered how to convert from a fraction no to a binary number.
Just mention how much u have to complete this.
Based on that if i have time i will help u!

well I really don't have much, just the code up above which doesn't work, and the one at the very top which doesn't convert anything passed the decimal point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.