setlocale(LC_ALL, "English"); does not work in Visual Studio 2019

Asked

Viewed 2,230 times

0

Good night to you all,

I did a lot of research on how to solve the Portuguese language accentuation problem that occurred shortly after upgrading to windows 10 but to date unsuccessful. I will post my code here for analysis, I really want to solve this problem if you can help me I appreciate it immensely. o modelo do projeto que sempre utilizo para praticar

#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <locale.h>
#include <cstdlib>
#include <clocale>
#include <Windows.h>
int main()
{
    //Declaração das variáveis.
    int getch(void);
    int indice=0;
    float media;
    char nome[40];
    char letra = 's';
    float nota[3];
  
    //Inicializando as variáveis.
    media = 0;
   
    while (letra == 's' || letra == 'S')
    {
        _locale_t _get_current_locale(void);
        setlocale(LC_ALL, ".OCP");
        printf("Nome do %d aluno é: ",indice+1);
        rewind(stdin);
        gets_s(nome);

        for (int i = 0; i <= 2; i++)
        {
            printf("\nDigite a %d nota: ",i+1);
            scanf_s("%f", &nota[i]); 
        }
        media = (nota[0] + nota[1] + nota[2]) / 3;
        indice++;

        printf("\nA media do aluno %s eh: %.2f", nome, media);
        printf("\n\nDeseja consulta a nota do proximo aluno (s/n)? ");
        rewind(stdin);
        letra = _getche();
        system("cls");

    }
   
}

2 answers

1

There is nothing in upgrading to Windows 10 that will create accent issues. In fact since 2018 Windows 10 has the Terminal which is the right place to run console programs since then: accents, Unicode, Greek, Russian, Unicode with all possible symbols. And graphics acceleration using the graphics card. And True Color instead of 256 colors.

I didn’t understand your program. a "lot of research" you did included this address: Official documentation about the console and maybe this link, which is on every screen of Windows 10 console: new console features and that Experimental settings of the terminal

Nas propriedades da janela

Back to your show

        _locale_t _get_current_locale(void);

Whenever you run a program on the console and change something it is ethical and polite to copy the original configuration and restore on output. And that includes the locale, the code pages, the fonts and, of course, the colors. For a moment I thought I was doing this but it didn’t save anywhere so it didn’t do any good. Read the documentation on _get_current_locale() return a _locale_t ? And what did you do to him? Nothing. He said no.

 setlocale(LC_ALL, ".OCP");

That’s probably not a good idea. It’s like calling system() to do something. If you have a problem with locale changes the locale. with setlocale(). If you need a particular codepage use SetConsoleOutputCP() and configure the page you want. Before, of course, use GetConsoleOutputCP() and save the page in use and restore on output. Your program can make someone very upset if you don’t do this.

This command you used defines the locale as being the one that is configured in the control panel. But you know what it is? Normal is 850 and will not serve. Why not use normal "pt_BR.UTF-8"?

And in the program, you’re using C++, because you don’t do the simple and write

        SetConsoleOutputCP(1252);

What is the normal page here in BR? Or the 65001, Unicode?

What do you want with

        rewind(stdin);

It almost never works. You’re reading from the keyboard.

        letra = _getche();

What’s the point? The IDE already stops by itself at the end of the program. This routine is from the 1980s. It’s compiling a header derived from a Borland library, from the 1980s, only to read a fine print? Don’t need this.

        system("cls");

And system() to clear the screen is little useful because as soon as you type something in the IDE it will close the window so it makes no difference whether you cleaned the screen or not. And this routine is banned and persecuted in many places for security reasons.

An implementation of cls() in the official

 int            cls()
{   // limpa a tela no windows, do jeito oficial
    CONSOLE_SCREEN_BUFFER_INFO      info;
    HANDLE      H = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD       origem = { 0,0 };
    int         total;
    if (H == INVALID_HANDLE_VALUE) return -1;
    GetConsoleScreenBufferInfo(H, &info);
    int r = FillConsoleOutputCharacter(H, (TCHAR)' ',
        info.dwSize.X * info.dwSize.Y,
        origem, &total);
    int s = FillConsoleOutputAttribute(
        H, info.wAttributes,
        info.dwSize.X * info.dwSize.Y,
        origem, &total);
    SetConsoleCursorPosition(H, origem);
    return 0;
};  // end cls()

Normal console programs in Windows: takes a Handle to access the window, sees the color and font in use, and clears all buffer --- default is 9001 lines in Windows for decades --- and then puts the cursor at the beginning of the screen

-4

#include <locale.h>

main(void)
{
    setlocale(LC_ALL, "pt-BR");
...

Browser other questions tagged

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