Thread: Where is my mistake?

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    5

    Where is my mistake?

    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?
    Last edited by bluebird1221; 04-29-2013 at 10:03 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps you should delete line 27, if you want to eliminate printing of the input number.

    Another thing, toBase() only handles the integer part anyway, so you need to plan something else to print the fractional part.

    > if if type 23517.75 and base 16, i should get 5BDD.C
    Here's a teaser....
    16 * .75 is 12 (which is 0xC in hex)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    Quote Originally Posted by Salem View Post
    Perhaps you should delete line 27, if you want to eliminate printing of the input number.
    I can't believe i did not notice that.

    Thanks, will work on sorting the decimals now, and will post if a new problem occurs.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    I thought of changing (int n, int base) to float n, float base so toBase can handle decimal fractions, but i'm having problems with the operator % in line 14 as it doesn't work with float. I'm also having trouble implementing fmod due to string x. Any suggestions on how line 14 should look with fmodf?

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    Quote Originally Posted by bluebird1221 View Post
    I thought of changing (int n, int base) to float n, float base so toBase can handle decimal fractions, but i'm having problems with the operator % in line 14 as it doesn't work with float. I'm also having trouble implementing fmod due to string x. Any suggestions on how line 14 should look with fmodf?
    Nevermind this, it still doesn't change anything. Any suggestions on how to proceed with the decimal fractions?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well to get say .75 out of 23517.75, look up the modf function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    Quote Originally Posted by Salem View Post
    Well to get say .75 out of 23517.75, look up the modf function.
    One quick question, do i need to split the string in order for the whole thing to work?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is my mistake
    By Dave Couture in forum C Programming
    Replies: 8
    Last Post: 08-14-2012, 10:28 PM
  2. mistake of using awk
    By lehe in forum Linux Programming
    Replies: 6
    Last Post: 04-02-2009, 04:41 PM

Tags for this Thread