First off, i'm an utter noob in c++, i just started working on it and i have an assignment, i need to create a program that converts decimal to any other base (2-16). Also if the given number has more than 10 decimal places, only the first ten should show.
This is my code:
Code:
#include <iostream>
#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() {
int a;
float num;
cout << "";
cin >> num;
cout << "";
cin >> a;
toBase(num, a);
cout << "." << num << endl;
}
And i messed up at the decimals, for example, if if type 23517.75 and base 16, i should get 5BDD.C but instead i get 5BDD.23517.8, if i type 29672.9311 in base 4, i should get 13033220.3232113021, but i get 13033220.29672.9
Or if i type 10.5 and base 2, i should get 1010.1 but instead i get 1010.10.5
What should i do?