Thread: binary...prob.

  1. #1
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    binary...prob.

    hi ,
    we convert from int to hex by
    cout << hex << int_value;

    to octal
    cout << oct << int_value;
    to binary
    cout << binary << int_value;
    but i get the following error
    cc0ibaaa.o(.text+0x23):conver~1.cpp: undefined reference to `binary(int)'

    what is wrong?
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    can u post the entire code or at least a little bit more?

  3. #3
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    umm..

    well,
    actually that is all the code.
    Code:
    #include <iostream>
    int main(void)
    {
    int int_value;
    
    cout << binary << int_value;
    }
    and the error is
    cc0ibaaa.o(.text+0x23):conver~1.cpp: undefined reference to `binary(int)'

    i use dev compiler.
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    wtf

    first of all you must return a value when using int main
    put return 0;
    like this

    Code:
    #include <iostream>
    int main(void)
    {
    int int_value;
    
    cout << binary << int_value;
    return 0;
    }
    and second what are you hoping the compiler will not just
    put the binary of your int_value if you havent told the program how to change the value to binary
    is this what you are trying to do?

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    hex and oct are ANSI standard ios class manipulators, binary is not. Unless your compiler has a special, what you are trying will not work.

    You can, of course, write your own manipulators.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>first of all you must return a value when using int main
    >>put return 0;
    It's been discussed many times before.... return 0; is not required under C++. As for the matter of style... well that's your choice. Do a search if you want to know more.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    One way to do it is with a bitset object:
    Code:
    #include <bitset>
    #include <iostream>
    #include <limits>
    using namespace std;
    
    int main
    {
        cout << "267 as unsigned long in binary form: "
             << bitset<numeric_limits<unsigned long>::digits>(267)
             << endl;
    
        return 0;
    }
    Barring any mistakes, this should output something like:
    267 as unsigned long in binary form: 00000000000000000000000100001011
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Here are some functions that handle any numeric base up to base-36:

    For input:
    strtoul() ANSI (string to long)
    strtod() ANSI (string to double)

    For output:
    ultoa() NON-ANSI Microsoft (unsigned long to string)

    Someday I'm gonna play around with these and make some example programs. For now, you're on your own (plus a little help from all the other smart people here!)

  9. #9
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Here's a little bit of an expansion to hk_mp5's code. Really all it does is make the "cout <<" statement shorter.

    Code:
    class bin {
      int value;
    
    public:
      explicit bin(int baseten) : value(baseten) { }  
      ostream& operator()(ostream &out) const;
    };
    
    ostream& bin::operator()(ostream &out) const {
      return out << bitset<8>(value);// change 8 to adjust # digits
    }
    
    ostream& operator<<(ostream &out, bin baseten) { return baseten(out); }
    
    int main() {
      int val;
      cout << "Enter a number: ";
      cin >> val;
    
      cout << val << " in binary is " << bin(val) << endl;
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM