I've been able to create a code that converts decimal numbers (whole numbers) to base 2 to 16 numbers, but i'm having trouble with the fractional part for quite a while.
#include <iostream>
#include <stdio.h>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
void toBase (int n, int base)
{
string x = "0123456789ABCDEF";
if (n > 0)
{
toBase(n / base, base);
cout << x[n % base];
}
}
int main()
{
float num;
int a;
float fNum = 12.34;
int iPart = (int)fNum;
float fPart = fNum - (float)iPart;
cout << "";
cin >> num;
cout << "";
cin >> a;
toBase(num, a);
cout << ".";
toBase(fNum, a);
cout << endl;
}
And these are the results (10.5 and 2 are the decimal number and the base, the second number is what i should get, the third number is what i get):
10.5 - 1010.1 - 1010.1100
2
3.827 - 10.2110222122 - 10.110
3
82.7593 - 101.6744681322 - 101.13
9
7218.8192 - 5472.90139A8535 - 5472.11
11
8273.8 - 26B8.C - 26B8.C
15
23517.75 - 5BDD.C - 5BDD.C
16
92.33271 - 1011100.0101010100 - 1011100.1100
2
4095.839 - 7777.6554426416 - 7777.14
8
29672.9311 - 13033220.3232113021 - 13033220.30
4
9999.987 - 270F.FCAC083126 - 270F.C
16
Only two of the ten test results are correct. I have a deadline in a few hours for the assignment, so how should i proceed, what should i add or change?