简体   繁体   English

C++, If...Else 带字符串和特殊字符

[英]C++, If...Else with strings and special characters

I was trying to create a simple program to help my students to train German irregular verbs, but I have had problems with special characters and If statement.我试图创建一个简单的程序来帮助我的学生训练德语不规则动词,但我在处理特殊字符和 If 语句时遇到了问题。 Basically it does not recognise the ä ö ü and ß, and output is therefore the Else statement (¨Nicht Gut"). How could I fix it?基本上它不识别 ä ö ü 和 ß,因此 output 是 Else 语句 (¨Nicht Gut")。我该如何修复它?

#include <iostream>
#include <conio.h>
#include <locale.h>

using namespace std;


int main () {
   
   setlocale(LC_CTYPE, "German");
   string Antwort1;
   string Antwort2;
   string Antwort3;
   getline(cin, str);
   
   cout << str;

   cout << "Präsens von BEHALTEN (du)" << endl;
   cin >> Antwort1;
       
   if (Antwort1 == "behältst") {
       cout << "Gut!" << endl;
   }
   else {
       cout << "Nicht Gut" << endl;
   }
   
   cout << "Präsens von BEHALTEN (er/sie/es/man) " << endl;
   cin >> Antwort1;
       
   if (Antwort1 == "behält") {
       cout << "Gut!" << endl;
   }
   else {
       cout << "Nicht Gut" << endl;
   }

   return 0;   
}

I tried with我试过


if (Antwort1 == (LC_CTYPE, "German"),"behält")


but then it causes the contrary effect.但它会导致相反的效果。 Then every single string I write is valid ("Gut").然后我写的每个字符串都是有效的(“Gut”)。

My answer applies to the Windows 10 console using the classic default Command Prompt (I haven't tried it with other systems like PowerShell, nor I have tried these experiments on Linux yet).我的答案适用于使用经典默认命令提示符的Windows 10 控制台(我还没有在 PowerShell 等其他系统上尝试过,也没有在 Linux 上尝试过这些实验)。

It seems to me that, as of today (23 February 2022), Windows 10's Command Prompt and the Microsoft C/C++ Runtime of VS2019 don't support Unicode UTF-8 well : See, for example, this blog post showing a CRT crash you get when trying to call:在我看来,截至今天(2022 年 2 月 23 日),Windows 10 的命令提示符和 VS2019 的 Microsoft C/C++ 运行时不支持 Unicode UTF-8 :例如,请参阅这篇显示 CRT 崩溃的博文尝试致电时:

_setmode(_fileno(stdout), _O_U8TEXT);

and printing UTF-8 text using std::cout .并使用std::cout打印 UTF-8 文本。

In my experience, you can make Unicode work in Windows Command Prompt using Unicode UTF-16 .根据我的经验,您可以使用Unicode UTF-16使 Unicode 在 Windows 命令提示符中工作。 You can still use UTF-8 in your C++ application, but you have to convert between UTF-8 and UTF-16 at the Windows boundaries.您仍然可以在 C++ 应用程序中使用 UTF-8,但您必须在 Windows 边界处在 UTF-8 和 UTF-16 之间进行转换。

I modified your code to use Unicode UTF-16, and the code seems to work correctly when compiled with Visual Studio 2019, and executed inside the Windows Command Prompt:我修改了您的代码以使用 Unicode UTF-16,代码在使用 Visual Studio 2019 编译并在 Windows 命令提示符内执行时似乎可以正常工作:

// Used for _setmode calls
#include <fcntl.h>
#include <io.h>
#include <stdio.h>

// Console I/O with Unicode UTF-16 wcin, wcout and wstring
#include <iostream>
#include <string>

using std::wcin;
using std::wcout;
using std::wstring;

int main() {
    // Enable Unicode UTF-16 console input/output
    _setmode(_fileno(stdout), _O_U16TEXT);
    _setmode(_fileno(stdin), _O_U16TEXT);

    wcout << L"Präsens von BEHALTEN (du) \n";
    wstring Antwort1;
    wcin >> Antwort1;

    if (Antwort1 == L"behältst") {
        wcout << L"Gut! \n";
    } else {
        wcout << L"Nicht Gut \n";
    }
}

Note the use of L"..." to represent UTF-16 string literals, and the use of wchar_t -based std::wcout , std::wcin , and std::wstring instead of the char -based std::cout , std::cin and std::string .请注意使用L"..."表示 UTF-16 字符串文字,并使用基于wchar_tstd::wcoutstd::wcinstd::wstring而不是基于charstd::cout , std::cinstd::string

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

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