View Single Post
 04-29-2013, 02:46 AM PM User | #1 Newb1311 New to the CF scene   Join Date: Apr 2013 Posts: 3 Thanks: 0 Thanked 0 Times in 0 Posts Help with a homework assignment Hello, i am a noob in C++ as my nick says, and my homework for tomorrow is to make a c++ code that converts decimal into other bases (from 2 to 16). This is what i've made so far: Code: ```#include #include using namespace std; void toBase(int n, int base) { string alpha="0123456789ABCDEF"; if (n > 0) { toBase(n/base,base); cout << alpha[n%base]; } } main() { float num, b; cout << ""; cin >> num; cout <<""; cin >> b; toBase(num,b); cout << "." << b << endl; }``` And these are the results: 10.5 = 1010.1 = 1010.2 2 3.827 = 10.2110222122 = 10.3 3 82.7593 = 101.6744681322 = 101.9 9 7218.8192 = 5472.90139A8535 = 5472.11 11 8273.8 =26B8.C = 26B8.15 15 23517.75 =5BDD.C = 5BDD.16 16 92.33271 = 1011100.0101010100 = 1011100.2 2 4095.839 = 7777.6554426416 = 7777.8 8 29672.9311 = 13033220.3232113021 =13033220.4 4 9999.987 = 270F.FCAC083126 = 270F.16 16 The first numbers are the input (the decimal number and the base), the second number are what i should get in order for it to be correct, the third number is what i get. I messed up at the decimals, i tried with iomanip and setprecision to add 10 decimals (the assignment says that if a number has more than 10 decimal places, it should show only the first ten), but it doesn't work, they're always wrong (and always numbers). Any help will be appreciated.