working example for money_put

hi,

I tried to compile and run an example code for usage of money_put
that comes with my compiler, but all I got was an empty string
or segmentation fault.

I need an example to start with. It should conform to the standard
and actually work. Please let me know if you have tested it and with
what compiler.

TIA,
georg
LVL 3
georg74Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rstaveleyCommented:
I had a play with money_put after your last posting, using VC 7.1, with the following code:
--------8<--------
#include <locale>
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>

int main ()
{
      const std::string str("1002");
      const long double ld = 1002;

      typedef std::ostreambuf_iterator<char,std::char_traits<char> > Itr;
      typedef std::money_put<char,Itr> MoneyPut;
      Itr cout_itr(std::cout);

      // Get a money put facet for std::locale()
      const MoneyPut& mp = std::use_facet<MoneyPut>(std::locale());

      // Put out the string representation of the monetary value
      std::cout << "string " << str << " is displayed as ";
      mp.put(cout_itr,false,std::cout,' ',str);
      std::cout << '\n';

      // Put out the long double representation of the monetary value
      std::cout << "long double " << ld << " is displayed as ";
      mp.put(cout_itr,false,std::cout,' ',ld);
      std::cout << '\n';
}
--------8<--------

My output is:

string 1002 is displayed as 1002
long double 1002 is displayed as 1002

Which I guess means that std::locale() doesn't do anything pretty with money.
0
georg74Author Commented:
this is because you're using default locale(), which is C locale.
try using locale("en_US") or locale("de_DE") and see which output you'll get

i tried on my debian (sarge) box:
with locale("en_US") i get
"USD 1,234.56" and "$1,234.56"
which is correct

with locale("de_AT") i get
"EUR 1 234,56"
which is not correct, it should be EUR 1.234,56

on HP-UX i get segmentation fault.
anyway, one first has to test if a facet is available with has_facet()

georg
0
rstaveleyCommented:
Yes... I tried several locales, but couldn't find any locales, which are supported by my system. Windows has different locale named from Linux, but I've not had the time to find out what they are called. GNU 3.2 with Cygwin does core dumps. GNU 2.97 with FreeBSD and an old RedHat 7.1 Linux box in my vicinity pre-date #include <locale>. It sounds like you are getting there with Debian, though.

I've found on EE that when subjects concerning IOStreams and more specifically locales are raised, a screen saver with tumbleweed appears on my PC 8-)

> anyway, one first has to test if a facet is available with has_facet()

Shouldn't one first test whether the locale is avaiable. I've been crudely doing the following:
--------8<--------
int main (int argc,const char* argv[])
try
{
      if (argc != 2)
            return (std::cerr << "Usage: " << argv[0] << " \"locale\"\n"),1;

      typedef std::ostreambuf_iterator<char,std::char_traits<char> > Itr;
      typedef std::money_put<char,Itr> MoneyPut;
      Itr cout_itr(std::cout);

      //const MoneyPut& mp = std::use_facet<MoneyPut>(std::locale());
      const MoneyPut& mp = std::use_facet<MoneyPut>(std::locale(argv[1]));

      // ....etc...

}
catch (...) {
      std::cerr << "Invalid locale\n";
}
--------8<--------

...and looking for valid locales by trial and error... but there ought to be a way of enumerating locales.... I guess. Have you found out how to do that? [Sorry I'm giuving you more questions than answers!]
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

georg74Author Commented:
rstaveley,

I did a similar thing - in my test prog i have a loop
read locale name from cin, then test if it is available.

then i test if it has the money_put facet.
all sorts of problems arise, varying from system to system:

* return value from has_facet depends on the type of iterator!?!
it returns true for the default type, false for some other type (like char*)

* crash on use_facet

* crash on put

* empty string returned

* incorrect formatting

I did not expect such sloppiness in standard library implementations :-(

therefore, I'm looking for 100% standard compliant test code, if there is such thing.
then I can start debugging.

georg
0
rstaveleyCommented:
Presumably, it is reckoned to be up to the user to implement his own facet, including a library of stuff they missed out :-)

When you've used Java's NumberFormat, the [apparent?] lack of native support for numeric formatting in C++ IOStreams implementations is bewildering. IOStreams don't seem to get STL's red carpet treatment for standards compliance.

I think we have been using standards compliant test code, but I'm no language lawyer. I'm hoping someone steps into this thread and shows us both the errors or our ways 8-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
georg74Author Commented:
I've just found following:

in STLport (www.stlport.org), money_put is broken (or incomplete) at least for all (?) versions
before 5.0 (still beta), i.e. before 2004/05/28. So, all compilers using ("stable release" of) STLport have broken money_put.

georg
0
georg74Author Commented:
PS: STLport uses following to test money_put:
----8<----
// ...
  money_put<char> const& fmp = use_facet<money_put<char> >(loc);
  ostringstream ostr;
  ostr.imbue(loc);
  ostr << showbase;
  ostreambuf_iterator<char> res = fmp.put(ostr, true, ostr, ' ', 123456);
// ...
----8<----

georg
0
rstaveleyCommented:
EE has consistently been relatively weak when it comes to locales in my experience. Perhaps there aren't enough of us working in non-English locales? Thanks for the points, Georg.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.