-
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?
-
can u post the entire code or at least a little bit more?
-
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.
-
wtf :eek:
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?
-
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.
-
>>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.
-
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
-
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!)
-
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;
}