money_get class template — Facet for input of monetary values
template <typename charT, typename InputIterator = istreambuf_iterator<charT> > class money_get : public locale::facet { public: typedef charT char_type; typedef InputIterator iter_type; typedef basic_string<charT> string_type; explicit money_get(size_t refs = 0); iter_type get(iter_type s, iter_type end, bool intl, ios_base& f, ios_base::iostate& err, long double& units) const; iter_type get(iter_type s, iter_type end, bool intl, ios_base& f, ios_base::iostate& err, string_type& digits) const; static locale::id id; protected: virtual ~money_get( ); virtual iter_type do_get(iter_type begin, iter_type end, bool intl, ios_base& strean, ios_base::iostate& err, long double& units) const;| virtual iter_type do_get(iter_type begin, iter_type end, bool intl, ios_base& stream, ios_base::iostate& err, string_type& digits) const; };
The money_get
class
template is a facet for parsing monetary values from an input
stream. The money_get<char>
and money_get<wchar_t>
instantiations are standard. Example 13-27 shows a simple
use of money_get
and money_put
.
Example 13-27. Reading and writing monetary values
#include <iostream> #include <locale> #include <ostream> int main( ) { std::ios_base::iostate err = std::ios_base::goodbit; long double value; std::cout << "What is your hourly wage? "; std::use_facet<std::money_get<char> >(std::locale( )).get( std::cin, std::istreambuf_iterator<char>( ), false, std::cin, err, value); if (err) std::cerr << "Invalid input\n"; else { std::cout << value << '\n'; std::cout << "You make "; std::use_facet<std::money_put<char> >(std::locale( )).put( std::cout, false, std::cout, std::cout.fill( ), value * 40); std::cout << " in a 40-hour work week.\n"; } }
As with other facets, the public members call virtual,
protected members with the same name prefaced by do_
. Thus, to use the facet, call the
public function get
, which calls
do_get
. The description below is
for the virtual functions because they do the real work. Imagine
that for each virtual-function description, there is a corresponding
description for a public, nonvirtual function, such as:
iter_type
get
(iter_type
begin
, iter_type end
, bool
intl,
ios_base&
stream
, ios_base::iostate&
err, long double&
units) const
Calls do_get(begin
,
end
, intl
, stream
, err
, units)
The following are the virtual, protected members of money_get
:
virtual
iter_type
do_get
(iter_type
begin, iter_type end
, bool
intl
, ios_base&
stream, ios_base::iostate&
err
, long double&
units) const
, virtual
iter_type
do_get
(iter_type
begin, iter_type end, bool
intl
, ios_base&
stream, ios_base::iostate&
err
, string_type&
digits)
const
Reads characters in the range [begin
, end
) and interprets them as a
monetary value. If intl
is
true
, the value is parsed
using international format; otherwise, local format is used.
That is, the intl
value is
used as the Intl
template
parameter to moneypunct<char_type
, Intl>
. If a valid monetary value
is read from the input stream, the integral value is stored in
units
or is formatted as a
string in digits
. (For
example, the input "$1,234.56
" yields the units 123456
or the digits "123456
".) The digit string starts
with an optional minus sign ('-
') followed by digits ('0
'-'9
'), in which each character
c
is produced by calling
ctype<char_type>.widen(c)
.
If a valid sequence is not found, err
is modified to include stream.failbit
. If the end of the
input is reached without forming a valid monetary value,
stream.eofbit
is also
set.
If the showbase
flag
is set (stream.flags( )
&
stream
.showbase
is not 0
), the currency symbol is required;
otherwise, it is optional. Thousands grouping, if the local
format supports it, is optional.
The sign of the result is dictated by positive_sign( )
and negative_sign( )
from the moneypunct
facet.
The return value is an iterator that points to one past the last character of the monetary value.