473,800 Members | 2,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use stand-alone function in cout-statement

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_s tring(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_strin g.length() % 4)
{
os << binary_string;

return;
}

string::size_ty pe index = 0;

for(string::siz e_type i = 0; i < binary_string.l ength() / 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_represe ntation: ";
output_binary_s tring(cout, s);
cout << endl;

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

Thanks for any replies
Jul 22 '05 #1
3 2749
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_s tring(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_strin g.length() % 4)
{
os << binary_string;

return;
}

string::size_ty pe index = 0;

for(string::siz e_type i = 0; i < binary_string.l ength() / 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_represe ntation: ";
output_binary_s tring(cout, s);
cout << endl;

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


An easy way would be

# include <iostream>
# include <string>

class output_binary_s tring
{
private:
std::string s_;

public:

output_binary_s tring(const std::string &s)
: s_(s)
{
}

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

std::ostream &operator<<(std ::ostream &stream,
const output_binary_s tring &obs)
{
obs.print(strea m);

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

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

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

Jonathan
Jul 22 '05 #2

"Eric Lilja" <er************ *@yahoo.com> wrote in message
news:cq******** **@news.island. liu.se...
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_s tring(ostream& os, const string& binary_string)
binary_string(c onst 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_strin g.length() % 4)
{
os << binary_string;

return;
}

string::size_ty pe index = 0;

for(string::siz e_type i = 0; i < binary_string.l ength() / 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_represe ntation: ";
output_binary_s tring(cout, s);
cout << endl;

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

Thanks for any replies


-Mike
Jul 22 '05 #3

"Mike Wahler" wrote:

"Eric Lilja" <er************ *@yahoo.com> wrote in message
news:cq******** **@news.island. liu.se...
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_s tring(ostream& os, const string& binary_string)


binary_string(c onst 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_strin g.length() % 4)
{
os << binary_string;

return;
}

string::size_ty pe index = 0;

for(string::siz e_type i = 0; i < binary_string.l ength() / 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_represe ntation: ";
output_binary_s tring(cout, s);
cout << endl;

I would like to be able to cout << "binary representation: " <<
output_binary_s tring(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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2252
by: AstrA | last post by:
Hi All My forms are gonig to have a dark to green background and I want my labels to stand out. I'm gonig to use white for the colour of the actual labels, but you see a lot of the professional programs have a subtle drop shadow on the labels to make them stand out. Is there an easy non-ActiveX way of doing this?
25
2676
by: Ben | last post by:
Hi all! I learned Python as part of my university coursework... I enjoyed it. Now I'm just wondering how Python compares with asp, jsp, php and what not?? I don't have slightest knowledge about these languages... Are they more advanced than Python? Can Python be integrated with Dreamweaver? Hope some of you can get me out of confusion...
1
5893
by: gicio | last post by:
HI! I have a little problem: I have change the administrator password (Windows 2003 Server) which MS SQL 2000 use to login. And now the SQL server can't stand up. What should I do? thx!
0
1324
by: Peter Sands | last post by:
Hi, I have setup a stand-by db2 DB server, I want to test the log shipping method. Do I just issue a 'list history..' on the source server to get the logs I need to ship. Then copy those logs across to the stand-by server's log directory and issue a 'roll forward' on the stand-by server.
1
1551
by: Tony Do | last post by:
Hi every, Which version of DB2 (Work group Or Enterprise) can I use as a stanby server? Under Window Do I need Windows server enterprise version?
1
2957
by: E. Robert Tisdale | last post by:
Acronym Finder: http://it.acronymfinder.com/af-query.asp?string=exact&acronym=rtfm
6
1959
by: Robert Smith | last post by:
I am programming C in linux and i just need a quick and dirty way to make some text stand out. I would like to change the color maybe to red. I want to print out something like printf("Blah: This is my message."); Blah: This is my message. I would like blah to be red. if not red than maybe bold or some way for it to stand out. thanks
5
3083
by: John Creighton | last post by:
What does BCPL stand for is it one language or a family of languages. My bust guess it the B C Programming Languages. But that is just a short in the dark. I am also not sure what languages fall into this family I think sometimes Java is included and maybe other times it is not?
4
3296
by: Greg | last post by:
Regarding extensibility, e.g. EnvDTE what does "DTE" stand for? -- Greg McPherran www.McPherran.com
1
3097
by: =?Utf-8?B?U3R1YXJ0TA==?= | last post by:
The PC running Windows XP Home SP2 is put into Stand By. After coming out of Stand By, I try to start an internet connection, using Tiscali Broadband. A dialogue box is displayed with the message: "Cannot connect the phonebook entry. Error 756: This connection is already being dialled" Is this a problem with Tiscali or XP? What should be done to resolve it?
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10274
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.