i18n/loc1.cpp

The following code example is taken from the book
The C++ Standard Library - A Tutorial and Reference, 2nd Edition
by Nicolai M. Josuttis, Addison Wesley Longman, 2012
Copyright © 2012 by Pearson Education, Inc. and Nicolai M. Josuttis


#include <iostream>
#include <locale>
#include <exception>
#include <cstdlib>
using namespace std;

int main()
{
   try {
      // use classic C locale to read data from standard input
      cin.imbue(locale::classic());

      // use a German locale to write data to standard output
      // - use different locale names for Windows and POSIX
#ifdef _MSC_VER
      cout.imbue(locale("deu_deu.1252"));
#else
      cout.imbue(locale("de_DE"));
#endif

      // read and output floating-point values in a loop
      cout << "input floating-point values (classic notation): " << endl;
      double value;
      while (cin >> value) {
           cout << value << endl;
      }
   }
   catch (const std::exception& e) {
      cerr << "Exception: " << e.what() << endl;
      return EXIT_FAILURE;
   }
}