Use stand-alone function in cout-statement

Discussion in 'C++' started by Eric Lilja, Dec 20, 2004.

  1. Eric Lilja

    Eric Lilja Guest

    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
    Eric Lilja, Dec 20, 2004
    #1
    1. Advertising

  2. Eric Lilja wrote:
    > 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
    Jonathan Mcdougall, Dec 20, 2004
    #2
    1. Advertising

  3. Eric Lilja

    Mike Wahler Guest

    "Eric Lilja" <> wrote in message
    news:cq7knk$r2n$...
    > 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
    Mike Wahler, Dec 20, 2004
    #3
  4. Eric Lilja

    Eric Lilja Guest

    "Mike Wahler" wrote:
    >
    > "Eric Lilja" <> wrote in message
    > news:cq7knk$r2n$...
    >> 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
    Eric Lilja, Dec 20, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Punya Narra
    Replies:
    5
    Views:
    1,677
    Itai Raz
    Feb 17, 2004
  2. greg
    Replies:
    1
    Views:
    263
    Peter Strøiman
    Oct 28, 2003
  3. GS
    Replies:
    1
    Views:
    702
    Nanda Lella[MSFT]
    Apr 13, 2006
  4. shawn bright
    Replies:
    2
    Views:
    91
    shawn bright
    Dec 24, 2008
  5. Plotinus
    Replies:
    2
    Views:
    285
    Tad McClellan
    Dec 17, 2004
Loading...

Share This Page