What we do is include the libraries wchar.h, which includes support for broad character types, and the library locale.h, which includes support for different types of date, currency, text, etc. formats.
Just use the function:
setlocale(LC_ALL, "");
The one that will allow us to use in our program different types of characters that do not belong to the "standard", which can be printed normally.
You have to have the character encoding of the terminal, the same as the character encoding of your text file. If your text file is in UTF-8, the terminal must be in UTF-8. If it is in UCS-16, then the same, the terminal in UCS-16, etc...
The compiler, not taking into account the encoding of the source file, takes the string literals as is, and stores them in the executable's memory. And when you print them, they will be dumped, as written, to the terminal. If the terminal encoding is different from the file encoding, it will interpret the binary sequence of the string as different characters.
I think the easiest way to print these characters is to use the library by declaring it at the top as "#include" and then writing "" which contains the function "setlocale (LC_CTYPE, "Spanish")" , writing that function right after declaring the variables will already take characters with accents and even the letter 'ñ'
Thanks if it worked for me with C++ in DevC++ in 2022
#include<iostream>
#include<locale.h>//Librería acentos
using namespace std;
int main(){
setlocale(LC_CTYPE, "Spanish");//Idioma y acentos
int numero;
cout<<"Digite un número: ";
cin>>numero;
cout<<"\nEl número que digito es: "<<numero;
return 0;
}
You could do something like this:
What we do is include the libraries
wchar.h
, which includes support for broad character types, and the librarylocale.h
, which includes support for different types of date, currency, text, etc. formats.Just use the function:
The one that will allow us to use in our program different types of characters that do not belong to the "standard", which can be printed normally.
Result:
You could try invoking the characters
That is, invoke the character code that corresponds to the accented letter...
Codes:
á: 160 é: 130 í: 161 ó: 162 ú: 163 Á: 181 É: 144 Í: 214 Ó: 224 Ú: 223 ñ: 164 Ñ: 165
You have to have the character encoding of the terminal, the same as the character encoding of your text file. If your text file is in UTF-8, the terminal must be in UTF-8. If it is in UCS-16, then the same, the terminal in UCS-16, etc...
The compiler, not taking into account the encoding of the source file, takes the string literals as is, and stores them in the executable's memory. And when you print them, they will be dumped, as written, to the terminal. If the terminal encoding is different from the file encoding, it will interpret the binary sequence of the string as different characters.
The character á is this hexadecimal number, depending on the encoding:
If the E1 byte arrives at an MSDOS terminal with codepage 850 (Spain), it will paint a: ß
If we are in MSDOS 850, we would have to launch an A0 byte so that 'á' would come out.
Of all it is better to release bytes iso than bytes codepage-850 already deprecated.
In fact, in Linux, UTF-8 is the most standard.
http://www.vicente-navarro.com/blog/2008/06/15/juegos-de-caracteres-ascii-cp850-iso-8859-15-unicode-utf-8/
You must use the equivalents:
For example:
Canción
>>Canci\xA2n
I think the easiest way to print these characters is to use the library by declaring it at the top as "#include" and then writing "" which contains the function "setlocale (LC_CTYPE, "Spanish")" , writing that function right after declaring the variables will already take characters with accents and even the letter 'ñ'
It's just implementing a library.
And place inside the main a:
This will make any text you put in a cout read a (ñ, ó, etc)
Thanks if it worked for me with C++ in DevC++ in 2022