C++ iomanip Library - get_money Function



Description

This function accesses the input sequence by first constructing an object of type basic_istream::sentry (with noskipws set to false). Then (if evaluating the sentry object is true), it calls money_get::get (using the stream's selected locale) to perform both the extraction and the parsing operations, and adjusts the stream's internal state flags accordingly. Finally, it destroys the sentry object before returning.

It is used to extracts characters from the input stream it is applied to, and interprets them as a monetary expression, which is stored as the value of mon.

Declaration

Following is the declaration for std::get_money function.

template <class moneyT>
/*unspecified*/ get_money (moneyT& mon, bool intl = false);

Parameters

mon − Object where the monetary value is stored. moneyT shall be either long double or a basic_string instantiation.

intl − true for international representations, false otherwise. This is used internally to instantiate the proper moneypunct class.

Return Value

Unspecified. This function should only be used as a stream manipulator.

Errors are signaled by modifying the stream's internal state flags −

flag error
eofbit The input sequence has no more characters available (end-of-file reached).
failbit Either no characters were extracted, or the characters extracted could not be interpreted as a valid monetary value.
badbit Error on stream (such as when this function catches an exception thrown by an internal operation).When set, the integrity of the stream may have been affected.

Exceptions

Basic guarantee − if an exception is thrown, the object is in a valid state. It throws an exception of member type failure if the resulting error state flag is not goodbit and member exceptions was set to throw for that state.

Any exception thrown by an internal operation is caught and handled by the function, setting badbit. If badbit was set on the last call to exceptions, the function rethrows the caught exception.

Data races

Modifies mon and the stream object from which it is extracted.

Concurrent access to the same stream object may cause data races, except for the standard stream objects cin and wcin when these are synchronized with stdio (in this case, no data races are initiated, although no guarantees are given on the order in which extracted characters are attributed to threads).

Example

In below example explains about get_money function.

#include <iostream>     
#include <iomanip>      

int main () {
   long double price;
   std::cout << "Please, enter the price: ";
   std::cin >> std::get_money(price);

   if (std::cin.fail()) std::cout << "Error reading price\n";
   else std::cout << "The price entered is: " << price << '\n';

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Please, enter the price: 50
The price entered is: 50
iomanip.htm
Advertisements