Setlocale doesn't work for the console title

Oct 10, 2015 at 9:17pm
Hello, I am writing a simple calculator in lithuanian and I have to use the letters "ąčęėįšųūž". If I write the line setlocale(LC_all, "Lithuanian");, it works fine for output text, but if I set the console title to that, any lithuanian characters are turned into gibberish (the lithuanian characters are turned into the exact same nonsense, letter for letter, if I delete the setlocale line). It's as if the setlocale doesn't affect the console title. Help would be appreciated, thanks!

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    setlocale(LC_ALL, "Lithuanian");
    cout << "ąčęėįšųūž";
    SetConsoleTitle("ąčęėįšųūž");
    return 0;
}
Oct 10, 2015 at 10:02pm
Windows supports two ways of handling strings: ANSI (SetConsoleTitleA) and UTF-16 (SetConsoleTitleW). You have to define some preprocessor flags to let the Windows.h header know you want Unicode support and then make sure your string literals are wide string literals by prefixing them with L.

This should probably work:
1
2
3
4
5
6
7
8
9
10
11
#define UNICODE
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
    setlocale(LC_ALL, "Lithuanian");
    cout << "ąčęėįšųūž";
    SetConsoleTitle(L"ąčęėįšųūž");
    return 0;
}
Oct 11, 2015 at 8:36am
Your code generates an error in line 9: error: converting to execution character set: Illegal byte sequence.
Oct 11, 2015 at 10:16am
Make sure you encode your source file in UTF8 and that your compiler is set to understand UFT8 strings.
Oct 11, 2015 at 10:44am
How do I do that in Code::Blocks? I've set the editor to use the "default" encoding when opening files (before I did that, it was messing up the lithuanian characters in the code). Also under "Edit" > "File encoding" I've set it to "System default". If I set it to UTF-8, it turns the lithuanian characters into gibberish in the console, to similiar effect if I hadn't written the setlocale line. Under the compiler settings I can't find anything about file encoding or UTF-8 or anything like that.
Oct 11, 2015 at 11:51am
I don't know, I've never used Code::Blocks. Also, be aware that the Windows console host has very poor support for UTF-8/Unicode in general. You'd be best to just not use the console at all if you need to display non-ASCII text - use a graphics library to render real fonts to the screen instead.
Oct 11, 2015 at 12:22pm
I started learning C++ only about a week ago, I have no idea how to use a graphics library and such. I'll look into it though, thanks!
Oct 11, 2015 at 12:27pm
An easy to use graphics library for C++ is SFML:
http://www.sfml-dev.org/

There are plenty of tutorials available for it online.
Oct 11, 2015 at 12:40pm
> I've set the editor to use the "default" encoding when opening files
> Also under "Edit" > "File encoding" I've set it to "System default".

Leave those settings as they are and try modifying line 8 to:
1
2
3
// SetConsoleTitle("ąčęėįšųūž");
// SetConsoleTitle(L"ąčęėįšųūž");
SetConsoleTitleW( L"ąčęėįšųūž" );
Oct 11, 2015 at 12:40pm
Thank you for the help, I'll go figure it out now!
Topic archived. No new replies allowed.