?
Solved

working example for money_put

Posted on 2004-11-15
8
Medium Priority
?
532 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:georg74
  • 4
  • 4
8 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 12592212
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
 
LVL 3

Author Comment

by:georg74
ID: 12592409
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 12594655
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Author Comment

by:georg74
ID: 12603372
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
 
LVL 17

Accepted Solution

by:
rstaveley earned 1500 total points
ID: 12605942
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
 
LVL 3

Author Comment

by:georg74
ID: 12636548
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
 
LVL 3

Author Comment

by:georg74
ID: 12636554
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 12807693
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question