Why in C ++ debug accented words and c? cip appearing unconfigured?

2

When debugging the accented words deconfigure. How do I correct this?  Using Visual Studio.

int main(){

int apples = 50;

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


return 0;
}
    
asked by anonymous 03.10.2014 / 07:58

2 answers

3

Short answer

Because Windows was made on the basis of gambiarra and so the console uses a different encoding of the Visual Studio interface. If you make a program using GUI you will not have this problem. It also resolves if you encode using a DOS editor.

Complete answer

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

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

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

To solve you have four options:

  • Map the code of all characters in DOS mode and change accents to those codes ("You" would become something like "Voc \ 0x79").
  • Use a DOS editor or IDE. To this day, the command comes with the command edit and you can find Turbo C ++ versions somewhere.
  • Use linux. At least in distros I've used both the console and the windows use unicode utf8 and the accents work correctly.
  • Run your program in a prompt window configured with the chcp 1252 command. For this setting to take effect, the command prompt window should be using a font that supports this encoding (such as "Lucida Console", not "Raster Font")
  • If you decide on option 1, you can know this mapping by compiling and running a simple program like this 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;
    }
    
        
    04.10.2014 / 05:31
    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!

        
    02.07.2015 / 14:53