Solved

C++ output some Greek text to the console?

Posted on 2013-11-21
14
1,179 Views
Last Modified: 2013-11-26
I'm just playing with characters beyond basic the ASCII. I'd like to print some Greek text to the console. So far my test program doesn't work:

[edit: this website is making it difficult for me to insert the Greek text into the code sample. Just imagine some Greek characters there where all the question marks are.]
#include <string>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
  wcout << L"Greek text: ¿a¿¿¿a¿¿ END\n";
  wcout << L"More plain text\n";
}

Open in new window

It prints, "Greek text: " and then stops.
0
Comment
Question by:deleyd
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 39667603
Just wondering if you have selected "Use Unicode character set" for your project?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 125 total points
ID: 39667640
You will need to set the coneole's code page to 'Greek' in order to correctly output Greek letters, e.g.

#include <windows.h> // required
#include <string>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
  UINT unOld CP = GetConsoleCP();

  SetConsoleCP(1253); // Greek

  wcout << L"Greek text: ¿a¿¿¿a¿¿ END\n";
  wcout << L"More plain text\n";

  SetConsoleCP(unOldCP);

  return 0; // was missing
}
                                  

Open in new window


The Greek code page is http://msdn.microsoft.com/en-us/library/cc195055.aspx ("Code Page 1253 Windows Greek"), verify that it is installed under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage
0
 
LVL 12

Assisted Solution

by:trinitrotoluene
trinitrotoluene earned 375 total points
ID: 39668159
On Windows you can set the locale to Greek as shown below and once you have the fonts installed the following code should also work

      
        std::wcout.imbue ( std::locale ( "greek" ) );
	std:wcout<<"aß¿de¿¿¿¿¿¿¿µ¿¿¿ps¿t¿f¿¿¿\n";
        

Open in new window


Remember to compile your app as Unicode
0
 

Author Comment

by:deleyd
ID: 39668367
Here are 7 different tries of ways I found on the internet — and none of them work.
#include "stdafx.h"
#include <stdio.h>
#include "windows.h"
#include <iostream>
#include <string>
#include <locale>

#include <io.h>
#include <iomanip>
#include <fcntl.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    //BEGIN - SHOW ALL CHARS IN CURRENT CODE PAGE
      int c, n;

      cout << setw( 5 ) << "+";
      for (n = 0; n < 16; n++)
        cout << setw( 3 ) << n;
      cout << endl;

      for (n = 32, c = 32; c < 254; c++)
        {
        if ((n % 16) == 0)
          cout << setw( 5 ) << n;

        cout << setw( 3 ) << (char)c;

        if ((++n % 16) == 0)
          cout << endl;
        }
      cout << endl;
    //END

//try 1
    std::wcout.imbue ( std::locale ( "greek" ) );
    wcout << L"¿¿¿¿¿¿ ¿¿¿ (2)!" << endl; // valid output!

//try 2
    std::locale::global(std::locale(""));
    wstring japan = L"¿¿";
    wstring message = L"Welcome! Japan is ";

    message += japan;

    wprintf(message.c_str());
    wcout << message << endl;

//try 3
    SetConsoleCP(GetACP());
    SetConsoleOutputCP(GetACP());
    SetConsoleOutputCP(CP_UTF8);

    wcout << L"¿¿¿¿¿¿ ¿¿¿ (2)!" << endl; // valid output!

//try 4
    SetConsoleOutputCP(CP_UTF8);
    SetConsoleCP(65001);  // sets input code page 65001 = UTF-8
    static const char s[]="tränenüberströmt™\n";

//try 5
    char* locale = setlocale(LC_ALL, "English"); // Get the CRT's current locale.
    std::locale lollocale(locale);
    setlocale(LC_ALL, locale); // Restore the CRT.
    std::wcout.imbue(lollocale); // Now set the std::wcout to have the locale that we got from the CRT.
    std::wcout << L"¡Hola!";
    std::wcout << L"Testing unicode -- English -- ¿¿¿¿¿¿¿¿ -- Español." << std::endl;
        
//try 6
    _setmode(_fileno(stdout), _O_U16TEXT);
    std::wcout << L"Testing unicode -- English -- ¿¿¿¿¿¿¿¿ -- Español." << std::endl;


  	return 0;
}

Open in new window

All an abysmal failure. I'm thinking of maybe switching to a Windows Forms program. Maybe a TextBox can display wide or uncode characters better (or maybe not).
0
 
LVL 86

Expert Comment

by:jkr
ID: 39669197
Have you tried 'SetConsoleCP()'?
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 39670965
can you let me know what you get if you run just the following in your program. Just strip off everything else.

int ident = GetConsoleOutputCP();

Open in new window

0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 39670970
Try the following code on your machine and let me know the output.


#include "windows.h"
#include "iostream"
using namespace std;

int main()
{
	int bef = GetConsoleOutputCP();
	cout<<endl<<"CP Before : "<<bef<<endl;
	SetConsoleOutputCP(1253); // Greek
	
	std::cout.imbue ( std::locale ( "ell" ) );	
	std::string agkAlp = "aß¿de";

	
  
	cout<<hex<<(unsigned int)(unsigned char)(agkAlp[0])<<endl;
	cout<<(unsigned int)(unsigned char)(agkAlp[1])<<endl;
	cout<<(unsigned int)(unsigned char)(agkAlp[2])<<endl;
	cout<<(unsigned int)(unsigned char)(agkAlp[3])<<endl;
	cout<<(unsigned int)(unsigned char)(agkAlp[4])<<endl;
	int aft = GetConsoleOutputCP();
	cout<<endl<<"CP After : "<<aft<<endl;
  return 0;
}

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 39670971
ofcourse use the first 5 characters of the Greek alphabet for the string agkAlp instead of the junk that got printed out above
0
 
LVL 12

Assisted Solution

by:trinitrotoluene
trinitrotoluene earned 375 total points
ID: 39670993
The problem is with the fonts that are used by your console. You need to set your console to use Unicode fonts.

Open a command prompt and type the following

chcp 1253

Open in new window


Now run the executable from this command line. My program below worked fine

#include "windows.h"
#include "iostream"
using namespace std;

int main()
{	
		 
	SetConsoleOutputCP(1253); // Greek
	
	std::cout.imbue ( std::locale ( "ell" ) );	
	std::string agkAlp = "aß¿de";  
	
	cout<<agkAlp.c_str()<<endl;
	
  return 0;
}

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 39671585
trinitrotoluene, you don't care the least that the CP issue had already been mentioned by others?
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 39672184
jkr,  

I just now had a look at your solution.

I have used a different approach and API and have mentioned the console fonts in my last comment which I believe is the root cause. I don't see you mentioning console fonts.

Anyway deleyd seems to have found little success with your solution.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39672210
Well, the API doesn't differ much, yet I am inclined to recommend the asker to re-check if that code page is installed if both approaches don't work. Anyway, it's a good practise to read all comments before contributing, just to avoid any confusion.
0
 
LVL 12

Accepted Solution

by:
trinitrotoluene earned 375 total points
ID: 39679549
deleyd, has any progress been made at all with any of the suggestions?

In summary the approach you are using is not recommended. Resource files which externalize Unicode strings is usually the approach taken
0
 

Author Closing Comment

by:deleyd
ID: 39679798
Decided to abandon the idea and move to a Windows Forms program. TextBox in Windows Forms seems to support Unicode (or at least wide characters) fine. Thank you for all the suggestions.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C language IDE – Compilers installation 14 68
Move constructor only called if marked noexcept? 6 98
C++ Language error 28 197
Copy output image from TWindowsMediaPlayer 6 23
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

867 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now