【问题标题】:Can't get french characters to work in C++无法让法语字符在 C++ 中工作
【发布时间】:2019-02-15 12:34:07
【问题描述】:
// francais projecct test1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
    char  userAnswer[10];
    char answer[] = { "Vous êtes" };

    wcout << "s'il vous plaat ecrire conjugation pour Vous etre: ";

    cin>>userAnswer;

    if (strcmp(userAnswer, answer) == 0)

        cout << endl << "correct"<<endl<<endl;
    else
        cout << endl << "wrong answer"<<endl<<endl;

    system("pause");
    return 0;
}

编译器无法识别重音字符,如果需要 unicode,我不知道如何获取 unicode 字符的输入。

【问题讨论】:

  • P.S. “plaît”有一个抑扬符,“écrire”有一个锐音。
  • 虽然我通常会为您尝试在 Windows 上使用 Unicode IO 鼓掌,但如果您只对法语口音(和控制台输出)感兴趣,那么您不需要:您可以涵盖大多数欧洲的默认代码页。
  • 我的法语真的很生疏,但我也突然想到:无论如何,你可能想要“écrivez”,而不是“écrire”,作为命令式的 vous 形式。
  • 结果程序只回答“是”,即使答案错误并且重音字符在终端中无法识别

标签: c++ unicode


【解决方案1】:

std::getline 是为std::basic_string 定义的(特殊情况包括std::stringstd::wstring)。普通字符数组不属于该类别。

参考: http://www.cplusplus.com/reference/string/string/getline/

虽然我强烈建议你使用std::string / std::wstring,但如果你想让你的代码工作,你必须在你的情况下使用cin.getline

您可以参考示例 2: https://www.programiz.com/cpp-programming/library-function/iostream/wcin

其次,userAnswer == answer 是错误的,因为它会比较两个指针,而不是它们的实际内容。

为此,您应该使用strcmp()

参考:http://www.cplusplus.com/reference/cstring/strcmp/

类似这样的:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
    char userAnswer[10];
    char answer[] = "Vous etes";

    wcout <<"s'il vous plait ecrire conjugation pour Vous etre: ";
    cin.getline(userAnswer, 10);

    if (!strcmp(userAnswer, answer))
    {
        wcout <<endl<< "correct";
    }
    else
    {
        wcout <<endl<< "wrong answer";
    }

    return 0;
}

【讨论】:

  • 我如何让 userAnswer == answer 工作并让它比较他们的实际内容
  • @Juan 你想使用char * 还是wchar_t *?我对每两个都有单独的答案。
  • 我实际上不知道该使用哪个我使用 char * 以减少红色错误标记这是我第一次与 unicode 交互,哪个更好 wchar_t 或 char 你能告诉我
  • 这实际上取决于编码的类型和平台。看到这个:stackoverflow.com/a/17871934/5859925。所以,我认为char * 就足够了。我会相应地回答。
  • "std::getline 是为 std::string 定义的。" 是错误的。根据documentationstd::getline 不绑定到特定类(std::stringstd::wstring)或特定流(std::cinstd::wcin)。
猜你喜欢
  • 1970-01-01
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多