473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

skipws and <<

This is something that has puzzled me for a while. How does C++ define
skipws and noskipws
in such a way that they can be used as pseudo-variables that setf and unsetf
a flag?

The reason I ask is that I am defining a class for unlimited-length integers
and have defined a
friend operator overload of << and I would like the user to be able to turn
on and turn off the
printing of commas delimiting groups of three digits.

I am at a loss as to how this can be done.
Dec 13 '05 #1
2 2179
On Tue, 13 Dec 2005 18:17:05 -0500 in comp.lang.c++, "Norman Landis"
<nl*****@optonline.net> wrote,
This is something that has puzzled me for a while. How does C++ define
skipws and noskipws
in such a way that they can be used as pseudo-variables that setf and unsetf
a flag?


The basic idea: they are functions, and operator<< is overloaded for
function types to apply the function on the stream.

Dec 14 '05 #2
Norman Landis wrote:
This is something that has puzzled me for a while. How does C++ define
skipws and noskipws
in such a way that they can be used as pseudo-variables that setf and unsetf
a flag?

The reason I ask is that I am defining a class for unlimited-length integers
and have defined a
friend operator overload of << and I would like the user to be able to turn
on and turn off the
printing of commas delimiting groups of three digits.


Here's a way to allow the user to turn on and off the printing of
commas. With a system like this in place, you can just write:
ExtendedInteger ei(100);
cout << noCommas << ei << endl;
cout << commas << ei << endl;

I have not implemented a full extended integer type, just the basics
necessary to demonstrate the use of the noCommas and commas things.

/* file commaprint.hpp */

#ifndef H_COMMAPRINT
#define H_COMMAPRINT

#include <iostream>

class Comma {};

class NoComma {};

extern Comma commas;
extern NoComma noCommas;

class ExtendedInteger
{
private:
long value;

public:
ExtendedInteger(long v);

friend std::ostream&
operator<<(std::ostream& s, const ExtendedInteger& ei);

ExtendedInteger& operator/=(const ExtendedInteger& rhs);
ExtendedInteger operator% (const ExtendedInteger& rhs) const;
bool operator!=(const ExtendedInteger& rhs) const;
int getIntValue() const;
};

std::ostream&
operator<<(std::ostream& s, const Comma& c);

std::ostream&
operator<<(std::ostream& s, const NoComma& nc);

#endif

/* end file commaprint.hpp */


/* file commaprint.cpp */

#include "commaprint.hpp"

#include <iostream>
#include <string>

static bool enableCommas;

Comma commas;
NoComma noCommas;

std::ostream&
operator<<(std::ostream& s, const Comma& c)
{
enableCommas = true;
return s;
}

std::ostream&
operator<<(std::ostream& s, const NoComma& nc)
{
enableCommas = false;
return s;
}

ExtendedInteger::ExtendedInteger(long a)
: value(a)
{}

std::ostream&
operator<<(std::ostream& s, const ExtendedInteger& ei)
{
ExtendedInteger a = ei;
std::string output;
char buf[5];
const char *format = enableCommas ? "%03d," : "%03d";
do
{
sprintf(buf, format, (a % 1000).getIntValue());
output.insert(0, buf);
a /= 1000;
}
while(a != 0);
if(enableCommas) output.erase(output.end() - 1);
if((output.begin())[0] == '0') output.erase(output.begin());
if((output.begin())[0] == '0') output.erase(output.begin());
return s << output;
}

ExtendedInteger&
ExtendedInteger::operator/=(const ExtendedInteger& rhs)
{
value /= rhs.value;
return *this;
}

ExtendedInteger
ExtendedInteger::operator% (const ExtendedInteger& rhs) const
{
return value % rhs.value;
}

bool
ExtendedInteger::operator!=(const ExtendedInteger& rhs) const
{
return value != rhs.value;
}

int
ExtendedInteger::getIntValue() const
{
assert(value >= INT_MIN && value <= INT_MAX);

return int(value);
}

/* end file commaprint.cpp */


/* file test_driver.cpp */

#include "commaprint.hpp"

#include <iostream>

using namespace std;

int main(void)
{
ExtendedInteger i(1000000000);
do
{
i /= 10;
cout << " " << noCommas;
cout.width(12);
cout << i << " " << commas;
cout.width(14);
cout << i << endl;
}
while(i != 0);
return 0;
}

/* end file test_driver.cpp */

--
Simon.
Dec 14 '05 #3

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

Similar topics

1
by: Christian Schmidbauer | last post by:
Hello! I prepare my XML document like this way: ------------------------------------------------------- PrintWriter writer; Document domDocument; Element domElement; // Root tag
2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
11
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
10
by: Jon Noring | last post by:
Out of curiosity, may a CDATA section appear within an attribute value with datatype CDATA? And if so, how about other attribute value datatypes which accept the XML markup characters? To me,...
9
by: Eric Lindsay | last post by:
I can't figure how to best display little snippets of shell script using <pre>. I just got around to organising to bulk validate some of my web pages, and one of the problems occurs with Bash...
9
by: sshock | last post by:
Hi all, I want to read from a file into a vector<unsigned char>. Right now my code looks like this: FILE* f = fopen( "datafile", "rb" ); enum { SIZE = 100 }; vector<unsigned char>...
6
by: tentstitcher | last post by:
Hi all: I have a source xml document with an element of type string. This element contains something like the following: <stringData> &lt;Header&gt; &lt;Body&gt; </stringData> I would like to apply an...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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,...

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.