Thread: print accented character to console

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    16

    print accented character to console

    Hello ,

    I want to print accented character to console. There are 4 examples.
    It's only the 3th example works.
    The 4th doesnt but it has differents issues.
    pls let me know if it isn't clear. Thanks

    Code:
    // Console char set code
    #include "pch.h"
    #include <iostream>
    #include <iterator> 
    #include "windows.h"
    
    
    UINT myCharSetCode;
    using namespace std;
    
    
    int main()
    {
        struct poetry {
            string author;
            string title;
            string text;
            wchar_t text_wchar_t;
        };
        poetry proemio;
        poetry *p;
        p = &proemio;
    
    
        p->author = "Omero";
        p->title = "Odissea";
        p->text = "Narrami, o musa , l'uomo dall'agile mente che a lungo and vagando, ";
    
    
        myCharSetCode = GetConsoleCP();  //it's 437
        /*
        https://en.wikipedia.org/wiki/Code_page_437
        */
    /******************************************************************************************/
        /* Example n1*/
        cout << "This is the " << myCharSetCode << " char set code." << endl;
    
    
        cout << "\x85 = 133 but cout cant prints " << endl; // that is it prints α
        cout << "\xA0 = 160 but cout cant prints " << endl; // that is it prints 
    
    
        cout << "\x8A = 138 but cout cant prints " << endl; // i didnt find
        cout << "\x82 = 130 but cout cant prints " << endl; // that is it prints Θ
    /******************************************************************************************/
        /* Example n2*/
        for (char c : p->text){  // another example
            cout << c;    // Here, cout prints wrong char. It cant print ''
        }
            
    /*****************************************************************************************/
        /* Example n3*/
        /* a working example */
        setlocale(LC_ALL, ".437");
        wcout << L"\n   \n";
    /***************************************************************************************************/
         /* Example n4*/
        /* Trying to print wchar_t of the struct */
         setlocale(LC_ALL, ".437");
    
        p->text_wchar_t = ""; // cant assign, error. It's an error
    
        //wcout << ...... 
    }
    Code:
    /* Example n4*/
        /* Trying to print wchar_t of the struct */
         setlocale(LC_ALL, ".437");
    
        p->text_wchar_t = ""; // cant assign, error. It's an error
    
        //wcout << ......

    1) How Do I assign a string to wchar_t which is part of a struct ?


    2) Is this the 3th example the 'official way' to print to console or I'm making it harder?

    3) I'm reading about char code , because I dont know it,
    but I've another question.
    If I have the same code on console (437) and I need that code set to print my chars ,
    Why Don't the first and second example work ?

    I didnt used SetConsoleOuput because that code is what I need
    Last edited by Lag; 04-03-2019 at 03:44 PM.

  2. #2
    Registered User
    Join Date
    Mar 2019
    Posts
    16
    I've found the solution but I don't know if it's the best

    Code:
    struct poetry {
    		string author;
    		string title;
    		string text;
    	};
    	poetry proemio;
    	poetry *p;
    	p = &proemio;
    Code:
    /* Example n5 */
    	wstring test(L"Test : caff        ");
    	string  wText(test.begin(), test.end());
    	p->text = wText;
    
    
    	for (char c : p->text) {  
    		wcout << c;			 //It prints everything correctly
    	}
    I have to convert to unicode in order to print to console.
    I dont know if it's possibile do the same thing with wchar struct member (?)

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    16
    I'm sorry I've noticed I messed up this topic.
    This post cancel and replace the previous.


    I want to print some accented character to console.
    The only way I've found is to convert string to wstring.
    However, I was unable to use wstring with Struct.
    Just to round it up I want to say that finally I managed to solve the problem:


    Code:
    #include "pch.h"
    #include <iostream>
    
    
    using namespace std;
    
    
    
    
    int main()
    {
        struct poetry {
            string author;
            string title;
            string text;
            wstring w_text;
        };
        poetry proemio;
        poetry *p;
    
    
        p = &proemio;
    
    
        p->author = "Omero";
        p->title = "Odissea";
        p->w_text = L"    tst";
    
    
        setlocale(LC_ALL, ".437");
        for (wchar_t c : p->w_text) {
            wcout << c;    //It prints everything correctly
        }
    
    
        return 0;
    437 is the console char set
    bye

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accented Characters
    By JosepfhW in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2015, 05:47 PM
  2. Arrays - accented characters
    By FernandoBasso in forum C Programming
    Replies: 11
    Last Post: 11-02-2011, 01:58 PM
  3. print * instead of character
    By behzad_shabani in forum C++ Programming
    Replies: 14
    Last Post: 10-03-2008, 04:07 PM
  4. print hex value of character
    By Abda92 in forum C++ Programming
    Replies: 6
    Last Post: 09-10-2008, 04:28 PM
  5. How to print Right aligned Text in a console?
    By Aidman in forum C++ Programming
    Replies: 13
    Last Post: 03-09-2003, 10:43 PM

Tags for this Thread