Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Help with a homework assignment

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 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.
 04-29-2013, 10:56 AM PM User | #2 felgall Master Coder     Join Date: Sep 2005 Location: Sydney, Australia Posts: 6,099 Thanks: 0 Thanked 579 Times in 569 Posts It looks like it is the fraction part that is always wrong - possibly because floating point numbers are never exact when it comes to number bases other than 2. Have you considered multiplying by 1e10 first before converting and then shifting the decimal point back to the right place afterwards? __________________ Stephen Learn Modern JavaScript - http://javascriptexample.net/ Helping others to solve their computer problem at http://www.felgall.com/ Beginners need to advise whether they want to learn "Latin" JavaScript for Netscape 3 or "Italian" JavaScript for modern browsers.
04-29-2013, 03:14 PM   PM User | #3
Newb1311
New to the CF scene

Join Date: Apr 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
 Originally Posted by felgall It looks like it is the fraction part that is always wrong - possibly because floating point numbers are never exact when it comes to number bases other than 2. Have you considered multiplying by 1e10 first before converting and then shifting the decimal point back to the right place afterwards?
When i tried that i got

p_60133.cpp: In function ‘int main()’:
p_60133.cpp:17:16: warning: overflow in implicit constant conversion [-Woverflow]

and the same results.

 04-29-2013, 04:48 PM PM User | #4 Newb1311 New to the CF scene   Join Date: Apr 2013 Posts: 3 Thanks: 0 Thanked 0 Times in 0 Posts My mistake is that i'm entering a float, which will be cast in to an integer when calling the toBase function. Hence, the fractional part of the number is gone at this moment. Then, in the very last line of main, i output "." << b, b being the base. However, i'm still stumped and don't know how to proceed, should i change the int in toBase to float, and then use fmod for this part? toBase(n/base,base); cout << alpha[n%base];

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 11:23 AM.