Use stand-alone function in cout-statement

E

Eric Lilja

Hi, I need a function that expects a const std::string& containing the
visual representation of a number in binary format. It should then output
this number in groups of four bits separated by a space. If the length of
the string is not divisable by four (normally, it should receive 8-bit
numbers (that is, strings of length 8), it should output the number as-is,
no grouping). I came up with:

void
output_binary_string(ostream& os, const string& binary_string)
{
/* If the length of the string (i.e., the number of bits in *
* the binary number), isn't divisable by four, simply output *
* the entire string and return. */
if(binary_string.length() % 4)
{
os << binary_string;

return;
}

string::size_type index = 0;

for(string::size_type i = 0; i < binary_string.length() / 4; ++i)
{
for(int j = 0; j < 4; j++)
{
os << binary_string[index++];
}

os << ' ';
}
}

Now, what if I want to use this function in a cout-statement? Do I have to
create a class or struct then and make my function the overloaded operator<<
for that class/struct?
Right now I do:
cout << "binary_representation: ";
output_binary_string(cout, s);
cout << endl;

I would like to be able to cout << "binary representation: " <<
output_binary_string(cout, s) << endl;

Thanks for any replies
 
J

Jonathan Mcdougall

Eric said:
Hi, I need a function that expects a const std::string& containing the
visual representation of a number in binary format. It should then output
this number in groups of four bits separated by a space. If the length of
the string is not divisable by four (normally, it should receive 8-bit
numbers (that is, strings of length 8), it should output the number as-is,
no grouping). I came up with:

void
output_binary_string(ostream& os, const string& binary_string)
{
/* If the length of the string (i.e., the number of bits in *
* the binary number), isn't divisable by four, simply output *
* the entire string and return. */
if(binary_string.length() % 4)
{
os << binary_string;

return;
}

string::size_type index = 0;

for(string::size_type i = 0; i < binary_string.length() / 4; ++i)
{
for(int j = 0; j < 4; j++)
{
os << binary_string[index++];
}

os << ' ';
}
}

Now, what if I want to use this function in a cout-statement? Do I have to
create a class or struct then and make my function the overloaded operator<<
for that class/struct?
Right now I do:
cout << "binary_representation: ";
output_binary_string(cout, s);
cout << endl;

I would like to be able to cout << "binary representation: " <<
output_binary_string(cout, s) << endl;

An easy way would be

# include <iostream>
# include <string>

class output_binary_string
{
private:
std::string s_;

public:

output_binary_string(const std::string &s)
: s_(s)
{
}

void print(std::eek:stream &stream) const
{
stream << s_; // just format it as you wish
}
};

std::eek:stream &operator<<(std::eek:stream &stream,
const output_binary_string &obs)
{
obs.print(stream);

return stream;
}


int main()
{
std::string s = "101001010";

std::cout << "Binary: " << output_binary_string(s) << std::endl;
}

You could work out something with manipulators also. Let us know if you
need help.

Jonathan
 
M

Mike Wahler

Eric Lilja said:
Hi, I need a function that expects a const std::string& containing the
visual representation of a number in binary format. It should then output
this number in groups of four bits separated by a space. If the length of
the string is not divisable by four (normally, it should receive 8-bit
numbers (that is, strings of length 8), it should output the number as-is,
no grouping). I came up with:

void
string

output_binary_string(ostream& os, const string& binary_string)

binary_string(const string& binary_string)
{
/* If the length of the string (i.e., the number of bits in *
* the binary number), isn't divisable by four, simply output *
* the entire string and return. */

ostringstream os;
if(binary_string.length() % 4)
{
os << binary_string;

return;
}

string::size_type index = 0;

for(string::size_type i = 0; i < binary_string.length() / 4; ++i)
{
for(int j = 0; j < 4; j++)
{
os << binary_string[index++];
}

os << ' ';
}

return os.str();
}

Now, what if I want to use this function in a cout-statement?


cout << binary_string("10010010") < '\n';
Do I have to
create a class or struct then and make my function the overloaded operator<<
for that class/struct?
Right now I do:
cout << "binary_representation: ";
output_binary_string(cout, s);
cout << endl;

I would like to be able to cout << "binary representation: " <<
output_binary_string(cout, s) << endl;

Thanks for any replies

-Mike
 
E

Eric Lilja

Mike Wahler said:
Eric Lilja said:
Hi, I need a function that expects a const std::string& containing the
visual representation of a number in binary format. It should then output
this number in groups of four bits separated by a space. If the length of
the string is not divisable by four (normally, it should receive 8-bit
numbers (that is, strings of length 8), it should output the number
as-is,
no grouping). I came up with:

void
string

output_binary_string(ostream& os, const string& binary_string)

binary_string(const string& binary_string)
{
/* If the length of the string (i.e., the number of bits in *
* the binary number), isn't divisable by four, simply output *
* the entire string and return. */

ostringstream os;
if(binary_string.length() % 4)
{
os << binary_string;

return;
}

string::size_type index = 0;

for(string::size_type i = 0; i < binary_string.length() / 4; ++i)
{
for(int j = 0; j < 4; j++)
{
os << binary_string[index++];
}

os << ' ';
}

return os.str();
}

Now, what if I want to use this function in a cout-statement?


cout << binary_string("10010010") < '\n';
Do I have to
create a class or struct then and make my function the overloaded operator<<
for that class/struct?
Right now I do:
cout << "binary_representation: ";
output_binary_string(cout, s);
cout << endl;

I would like to be able to cout << "binary representation: " <<
output_binary_string(cout, s) << endl;

Thanks for any replies

-Mike

Thanks both Mike and Jonathan for taking the time to help me out! I now
consider this problem solved and I've moved my attention to other issues in
this program. Thanks again.

/ Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top