Why in the debug of C++ accented words and c cedilla appearing uncompromised?

Asked

Viewed 1,100 times

2

When debugging the accented words they do not match. How do I correct this? Using Visual Studio.

int main(){

int apples = 50;

cout << "Há"<< apples <<"maçãs."<< endl;


return 0;
}
  • 2

    Related question on Soen. I don’t have enough experience to answer, but it seems to me that the use of accented characters in a C++ source code is "non-standard", so it can give different implementation results for implementation (including difference when using or not using Debugger). Your problem may be several: the use of another encoding (possibly CP850) instead of your native (probably Cp1252 or UTF-8), the lack of debugger source support for printed characters, etc.

2 answers

3

Short answer

Because Windows was made at the base of the gambiarra and so the console uses a different encoding of the Visual Studio interface. If you make a program using GUI you won’t have that problem. It also solves if you encode using a DOS editor.

Full answer

Because in DOS times, there was no Unicode yet. the ASCII standard was used, but it defines only the English alphabet, without accents support. There were, however, ways to extend the number of characters, but here each system defined a different encoding, sometimes more than one, as the DOS version in Portuguese that defined accents.

As it was a text-mode system, some characters were reserved for lines, boxes, etc., which could be used to form a simplified interface. When graphical interfaces became popular, there was no need for line symbols, boxes, etc., because the GUI gives you this. Soon a new codification was made, representing practically all Western languages. Unfortunately, the codes that represent the accents have changed and it is impossible to write a text in DOS and read in GUI without accents becoming illegible and vice versa.

MS could have remade the console to use the same encoding as the windows, but it was not a good economic option to highlight features for this, as it was focused on promoting graphical applications and was complicated issue that involved breaking back compatibility, so they left anyway and DOS was not even considered when Window joined the union.

To solve you have four options:

  1. Map the code of all characters in DOS mode and swap the accents for these codes ("You" would become something like "Voc 0x79").
  2. Use a DOS editor or IDE. Until today dos comes with the command edit and you can find versions of Turbo C++ somewhere.
  3. Use linux. At least in the distros I used both the console and the windows use utf8 Unicode and the accents work properly.
  4. Run your program in a prompt window configured with the command chcp 1252. For this setting to take effect, the command prompt window must be using a source that supports this encoding (such as "Lucida Console", not "Raster Font")

If you decide on option 1, you can know this by compiling and running a simple program like this one below:

#include <iostream>
using namespace std;
int main()
{     
   for(int i = 0x20; i <= 0xff; ++i){
       cout << "\\x"<< hex << i << "\t=> " << static_cast<char>(i) << endl;
   }  
   return 0;
}
  • Every accent letter I’d have to type in a code instead?

  • If I create a software in Visual Studio and do not do any tricks will always come out unnoticed when compiling?

  • Yes every accent letter would have to type a code, but only for text-mode programs. When you start doing programs in window mode you will no longer need to gambiarra.

  • 1

    I wouldn’t recommend using the codes like this. In addition to bad programming, Codepage-850 codes are obsolete, and if you want to run your program in a place that supports Unicode (like Linux) or at least Windows-1252, then you will have exactly the same problem only the other way around: the source code will have an encoding (CP850) and the terminal will have another (windows-1252, Unicode, whatever).

1

Use:

#include<tchar.h>

Then use:

_tsetlocale(LC_ALL, _T("portuguese"));

From now on you can send cout << "qq coisa com acentos"; that will work!

Browser other questions tagged

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