public member function
<locale>
iter_type get ( iter_type s, iter_type end, bool intl, ios_base& str,
ios_base::iostate& err, long double& units) const;
iter_type get ( iter_type s, iter_type end, bool intl, ios_base& str,
ios_base::iostate& err, string_type& digits) const;
Read monetary expression
Parses the sequence of characters between
s and
end for a monetary expression, and stores it into either
units or
digits.
The function extracts characters until the character extracted cannot be part of a valid monetary sequence expression or
end is reached. The next character in the sequence is pointed by the iterator returned by the function.
What constitutes a valid monetary sequence depends on the locale (according to
moneypunct<charT,intl>, where
intl is the third parameter of this function).
If successful, the first version stores a floating-point value with the interpretation of the monetary expression in
units. The second stores in
digits a
string object with the digits extracted (only the digits, no punctuation marks).
The function updates
err with the error status if necessary:
If the sequence of characters cannot produce any valid value as result according to its formatting rules, the function sets
err to
ios_base::failbit.
If the function exhausts the sequence of characters (i.e., it reaches
end) during its operations,
ios_base::eofbit is set in
err (both
failbit and
eofbit may be set by a single operation).
Otherwise,
ios_base::goodbit is set as
err's value, indicating success.
During its operation, the version of this function in the generic template simply calls the virtual protected member
do_get, which is the member function in charge of implementeing the behavior described above.
Parameters
- s, end
- Iterators pointing to the beginning and ending characters of the sequence. The range used is [s,end), which contains all the characters between s and end, including the character pointed by s but not the character pointed by end.
iter_type is a member alias of the second template parameter of num_get (i.e., the facet's iterator type). This can be any input iterator. By default, this is an istreambuf_iterator, allowing implicit conversions from istream objects.
- intl
- Value to be used as the international representation template argument for the corresponding moneypunct class template.
- str
- Object of a class derived from ios_base (generally an input stream object). It is used to obtain formatting information.
- err
- Stream error state object, of type ios_base::iostate where the resulting state will be stored.
- units
- The function stores in this variable the floating point value equivalent to the monetary expression read.
- digits
- The function stores in this variable a string object with the digits successfully extracted (only the digits, no punctuation marks).
Return value
The next character in the sequence right after where the extraction operation ended.
iter_type is a member alias of the second template parameter of
money_get (i.e., the facet's iterator type).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
// money_get example
#include <iostream>
#include <iterator>
#include <string>
#include <locale>
using namespace std;
int main ()
{
locale loc;
ios::iostate state;
long double price;
cout << "Please, enter the price: ";
use_facet<money_get<char> >(loc).get
(cin, istreambuf_iterator<char>(), false, cin, state, price);
if ((state&ios::failbit)==ios::failbit)
cout << "ERROR: failed to read price" << endl;
else
cout << "The price of three items is " << (price*3.0) << endl;
return 0;
}
|
Possible output:
Please, enter the price: $12.95
ERROR: failed to read price
|
The default locale could not interpret the format of that price.
See also
- money_put::put
- Format monetary expression (public member function)
- num_get
- Facet to parse numeric values (class template)