Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1511
  • Last Modified:

C++ output some Greek text to the console?

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
deleyd
Asked:
deleyd
  • 7
  • 4
  • 2
  • +1
4 Solutions
 
chaauCommented:
Just wondering if you have selected "Use Unicode character set" for your project?
0
 
jkrCommented:
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
 
trinitrotolueneDirector - Software EngineeringCommented:
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
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.

 
deleydAuthor Commented:
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
 
jkrCommented:
Have you tried 'SetConsoleCP()'?
0
 
trinitrotolueneDirector - Software EngineeringCommented:
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
 
trinitrotolueneDirector - Software EngineeringCommented:
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
 
trinitrotolueneDirector - Software EngineeringCommented:
ofcourse use the first 5 characters of the Greek alphabet for the string agkAlp instead of the junk that got printed out above
0
 
trinitrotolueneDirector - Software EngineeringCommented:
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
 
jkrCommented:
trinitrotoluene, you don't care the least that the CP issue had already been mentioned by others?
0
 
trinitrotolueneDirector - Software EngineeringCommented:
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
 
jkrCommented:
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
 
trinitrotolueneDirector - Software EngineeringCommented:
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
 
deleydAuthor Commented:
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

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.

  • 7
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now