简体   繁体   English

特殊字符有问题

[英]Trouble with special characters

First of all, I'm a newbie in programming so you might have to be patient with me. 首先,我是编程的新手,所以您可能需要耐心等待我。 The thing is, I'm writing a program that basically gets input and uses it to output some information plus the inputs in a .doc . 问题是,我正在编写一个基本上获取输入并使用它输出一些信息以及.doc输入内容的程序。

My problem is that I have some constant strings that output in a screwed up way when I use special characters like é í ó ã õ º ª . 我的问题是,当我使用诸如é í ó ã õ º ª类的特殊字符时,我有一些常量字符串会以一种错误的方式输出。

I was able to fix it by adding setlocale(LC_ALL, ("portuguese")) but then I screwed my outputs of inputs (aka variable strings) that doesn't print special characters any more. 我可以通过添加setlocale(LC_ALL, ("portuguese"))来解决此问题,但是后来我弄错了输入(也就是可变字符串)的输出,该输出不再打印特殊字符。 Any clues how i can solve this? 有什么线索我可以解决这个问题吗? I've already tried wstrings and looked everywhere but couldn't find a single solution. 我已经尝试过wstrings并四处查看,但找不到单个解决方案。

I can show my code here if it helps. 如果有帮助,我可以在这里显示我的代码。

Here is an example of my problem: 这是我的问题的一个例子:

#include <iostream>
#include <string>

using namespace std;

int main()
{
string a;
wcout << "Enter special characters like éíó: ";
getline (cin, a);
cout << a;

}

I can't make the constant string and the variable string output correctly in the console at the same time. 我无法同时在控制台中正确输出常量字符串和变量字符串。

You are probably using Windows. 您可能正在使用Windows。 The Windows' Command Prompt default encoding is CP850, this encoding is rarely used anywhere else and it will display most special symbols differently from what you usually see in your favorite text editor. Windows的命令提示符默认编码为CP850,该编码很少在其他任何地方使用,它显示的大多数特殊符号与您通常在喜欢的文本编辑器中看到的符号不同。 You can try to use the Windows APIs SetConsoleOutputCP(1252); 您可以尝试使用Windows API SetConsoleOutputCP(1252); and SetConsoleCP(1252); SetConsoleCP(1252); to change to CP1252, an encoding that is somewhat more compatible and should display those symbols the same way you see in the editor. 更改为CP1252,这是一种更加兼容的编码,应该以与在编辑器中相同的方式显示这些符号。 You will need the #include <windows.h> , if its available. 您将需要#include <windows.h> (如果可用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM