Dinkumware STL using Local

K

kenwilkinson

I use Dinkumware's STL with Borland C++ Builder 2006 (part of BDS
2006) to convert from a character array to a double in a Windows
application. This approach works well for English, but fails for
French as "0,1" (note the comma) is not interpreted as a decimal
point. What can I do to get setlocale() or Window's French regional
setting to work? Thanks. -Ken

#include <strstream>
double d;

setlocale(LC_ALL, "C");
SMove( d, "0.1"); // decimal point is a period. result: d = 0.1

setlocale(LC_ALL, "french");
SMove( d, "0,1"); // decimal point is a comma. result: d = 0

void SMove( double& dVal, char *sStr, int nLen )
{
istrstream s( sStr, nLen );
s >> dVal;
}
 
R

Robert Bauck Hamar

kenwilkinson said:
I use Dinkumware's STL with Borland C++ Builder 2006 (part of BDS
2006) to convert from a character array to a double in a Windows
application. This approach works well for English, but fails for
French as "0,1" (note the comma) is not interpreted as a decimal
point. What can I do to get setlocale() or Window's French regional
setting to work?

I don't know. This prints
0.1
0.1
on my computer though.

#include <strstream>
#include <locale>
#include <iostream>

void SMove( double& dVal, char *sStr, int nLen )
{
std::istrstream s( sStr, nLen );
s >> dVal;
}

int main()
{
double d;

std::locale::global(std::locale("C"));
SMove( d, "0.1", 3); // decimal point is a period. result: d = 0.1
std::cout << d << '\n';

std::locale::global(std::locale("fr_FR"));
SMove( d, "0,1", 3); // decimal point is a comma. result: d = 0
std::cout << d << '\n';
}
#include <strstream>
double d;

setlocale(LC_ALL, "C");
SMove( d, "0.1"); // decimal point is a period. result: d = 0.1

setlocale(LC_ALL, "french");
SMove( d, "0,1"); // decimal point is a comma. result: d = 0

void SMove( double& dVal, char *sStr, int nLen )
{
istrstream s( sStr, nLen );
s >> dVal;
}

I suggest you 1) provide compilable examples, and 2) read appendix D from
The C++ Programming Language, available from
<URL:http://www.research.att.com/~bs/3rd_loc0.html>.
 
K

kenwilkinson

Thanks, Robert. Your code worked perfectly. I'm unsure how to have
locale work properly from program startup. From what I read, the STL
should pickup the locale on its own, prior to main(). -ken
 
R

Robert Bauck Hamar

kenwilkinson said:
Thanks, Robert. Your code worked perfectly. I'm unsure how to have
locale work properly from program startup. From what I read, the STL
should pickup the locale on its own, prior to main().

The program will start up with the C locale. To get the user defined locale,
you can do:

std::locale loc(""); //user's preferred locale
// Set user's preferred locale as default for new streams.
std::locale::global(loc);

//make cin, cout ... use this
std::cin.imbue(loc);
std::cout.imbue(loc);
std::cerr.imbue(loc);
std::clog.imbue(loc);

Btw. 'STL' usually refers to other parts of the standard library than the
locale and streams libraries.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top