Conflict of accentuation in C

76 Views Asked by At

I'm trying to create a defensive read function that doesn't allow non-alphabetic characters to be input. It is working normally using <wchar.h>. However, there is a conflict when printing the string passed on to the reader. My locale is Portuguese, so I tried to put locale as Portuguese, then wprintf works, but the function no longer prints the accented characters in the string.

#include <stdio.h>
#include <stdlib.h>
#include <wctype.h>
#include <wchar.h>
#include <locale.h>


//Alphabetic character reader
void leitor_caractere(wchar_t *a){
    wchar_t string[100];
    int verificador=0;
    do{
        fgetws(string, 100, stdin);
        wcstok(string,L"\n",NULL);//removes \n from the string.
        for(int i=0;string[i]!=L'\0';i++){
            if((iswspace(string[i])!=0)||(iswalpha(string[i])!=0)){
                verificador=1;
            }else if(iswascii(string[i])==0){
                verificador=1;
            }else{
                wprintf(L"Digite somente letras!\n");//Warning to the user to write only letters or spaces
                verificador=0;
                break;
            }
        }
    }while(verificador==0);
    wcscpy(a,string);
}

int main(){
    wchar_t string[100];
    leitor_caractere(string);
    wprintf(L"Olá mundo, é, ó: %ls",string);

    return 0;
}

return of input "Olá José" without "set_locale(LC_ALL,"Portuguese")"::

Olß mundo, Ú, ¾: Olá josé

return of input "Olá José" with "set_locale(LC_ALL,"Portuguese")":

Olá mundo, é, ó: Ol  Jos'

Expected: "Olá mundo, é, ó:Olá José"

How can I adjust the code so that both can print the accents correctly?

0

There are 0 best solutions below