Solved

C++ output some Greek text to the console?

Posted on 2013-11-21
14
1,128 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 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

21 Experts available now in Live!

Get 1:1 Help Now