How to show accented characters in Visual Studio 2017 using C++?

Asked

Viewed 4,040 times

1

Good afternoon,

I asked this question a while ago, but it was bad so I deleted it and I’m redoing it in more detail.

My problem is this: When I accentuate in Visual Studio it returns strange characters. This only happens when I compile using Visual Studio, when I use Code::Blocks the output is normal.

Here’s the code:

#include "stdafx.h" //essa linha somente no Visual Studio
#include <iostream>
#include <cstdlib>
#include <clocale>
using namespace std;

int main()
{
    setlocale(LC_ALL, "Portuguese");
    cout << "ÁÉÍÓÚ" << endl;
    system("pause");
    return 0;
}

Here are the exits:

Visual Studio 2017:

Is it? Is it? Is it"

Code::Blocks 16.01:

ÁÉÍÓÚ

I’ve tried to use setlocale I tried to use the command chcp 850, 65001 and 1200 in this way: system("chcp xxx") where xxx is the code.

I changed the source to Lucida Consoles, also did not work.

I tried to change the source and executable to UTF-8 via command line with /source-charset:utf-8 /Execution-charset:utf-8 and it didn’t work.

There is an option to change the record but this is ridiculous. What’s more, Code::Blocks works smoothly, just adding setlocale.

Additional information:

OS: Windows 10 Home

I’ve used in the setlocale " ", "English", and more...

Any suggestions/ideas? Thank you.

Att;

  • I found the solution, I created an empty project and it worked, in addition to having fewer files like the stdafx.h header. I don’t know how to close the question, if anyone knows please feel free, thanks to everyone who has made their time available.

1 answer

1

The call of setlocale( LC_ALL, "" ); (with the second blank parameter), causes the default locale of the program to be set according to the variables of its environment, which certainly uses the encoding UTF-8 as a standard:

#include <iostream>
#include <locale>

int main(void)
{
    setlocale( LC_ALL, "" );

    std::wcout  <<  L"ÁÉÍÓÚ"  <<  std::endl;
}

The coding UTF-8 covers virtually all existing alphabets, making language specification an expendable detail.

#include <iostream>
#include <locale>


int main(){

    setlocale( LC_ALL, "" );

    std::wstring ch = L"你好世界";
    std::wstring gk = L"γειά σου κόσμος";
    std::wstring jp = L"こんにちは世界";
    std::wstring ko = L"여보세요 세계";
    std::wstring pt = L"Olá mundo!";
    std::wstring ru = L"Здравствулте мир!";

    std::wcout << L"Chinês    : " << ch << std::endl;
    std::wcout << L"Grego     : " << gk << std::endl;
    std::wcout << L"Japonês   : " << jp << std::endl;
    std::wcout << L"Coreano   : " << ko << std::endl;
    std::wcout << L"Português : " << pt << std::endl;
    std::wcout << L"Russo     : " << ru << std::endl;

}

Exit:

Chinês    : 你好世界
Grego     : γειά σου κόσμος
Japonês   : こんにちは世界
Coreano   : 여보세요 세계
Português : Olá mundo!
Russo     : Здравствулте мир!
  • Thank you for your reply. I know, what I didn’t understand was why no way to change the Unicode standard worked, but with the project blank setlocale works properly, I don’t know if this is a bug, but when I create a project Command Prompt, I can’t change Unicode, and I also have to include the header "stdafx.h".

  • Your command prompt certainly does not interpret UTF-8.

  • Here’s the funny thing: In Code::Blocks it works normally, also when I create an empty project in Visual Studio. Only the default template does not accept at all setlocale...

  • @uggorim Check the encoding of the files that the Visual Studio is generating! Certainly not encrypted files in UTF-8!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.