1

我想要的是显示用户输入的带有中文字母的行。虽然程序 DevC++。

这是我的代码:

#define UNICODE
#include <iostream>
#include <string>
    using namespace std;
    extern wostream wcout;
int main(int argc, char** argv) {
    std::wstring kanji = L"?";
    //std::wchar_t stop = L"0";
    std::wcout << L"\n\nWelcome to the Chinese letter program\n";
    std::wcout << L"The program will ask you for one Chinese letter\n";
    std::wcout << L"Then press enter, and a message will display.\n";
    std::wcout << L"Once you are done,you can enter another Chinese letter\n";
    std::wcout << L"To exit just close the Ubuntu's terminal'\n\n";
        for(int x = 0; x < 80; x++){
            std::wcout << L"Give me one Chinese letter...";
                wcin >> kanji;
                std::wcout << L"The Chinese letter is \"" << kanji << "\".\n\n";
        }
    return 0;
}

对我来说重要的是“中文字母是“(汉字)”。线。当我按照程序说的做时,我得到“中文字母是“?”。所以问题是 DevC++ 没有正确显示中文字母,即使我使用 wcin 和 wcout 的东西。

注意我在 Ubuntu 上通过 wine 使用 DevC++。

4

3 回答 3

0

你的控制台支持中文编码吗?看看这篇文章 如何打印一个汉字?

于 2020-06-03T03:27:04.787 回答
0

使用 html 解决了这个问题,以补充 DevC++ 不能做的事情,即处理中文字母。

c++ 代码最终是这样的:

#include <iostream>
#include <string>
    using namespace std;
int main(int argc, char** argv) {
    string kanji = "?";
    int x2=0;
    int x3=1;
    std::cout << "\n\nWelcome to the Chinese letter program\n";
    std::cout << "This program will display a line or lines with Chinese letters\n";
    std::cout << "through a jury-rigged method.\n";
    std::cout << "This will generate an html code, that must be\n";
    std::cout << "transformed into an html file.\n";
    std::cout << "To exit just close the Ubuntu's terminal.\n";
    std::cout << "Note: the only catch is that it must be applied\n";
    std::cout << "a backspace where you gave enter into the html code.\n\n";

    std::cout << "\n\nHow many Chinese letters will be?";
    cin >> x2;

    std::cout << "\n\n<html>\n";
    std::cout << "<header><title>Chinese letters</title></header>\n";
    std::cout << "<body>\n";
        for(int x = 0; x < x2; x++){
            std::cout << "<!-- (Loop " << x3 << "/" << x2 << ") -->\n";
            std::cout << "The Chinese letter is \"";
            cin >> kanji;
            std::cout << "\".\n";
            std::cout << "<br><br>\n";
            x3++;
        }
    std::cout << "</body>\n";
    std::cout << "</html>\n";
    std::cout << "\n\n";
    system("pause");
    return 0;
}

生成一个可以显示中文字母的html代码。例如像这样:

<html>
<header><title>Chinese letters</title></header>
<body>
<!-- (Loop 1/2) -->
The Chinese letter is "銀".
<br><br>
<!-- (Loop 2/2) -->
The Chinese letter is "囗".
<br><br>
</body>
</html>

以 c++ 程序的预期结束:

The Chinese letter is "銀".

The Chinese letter is "囗". 
于 2020-06-03T05:46:39.560 回答
0

如果不使用陪审团操纵的技巧,在 DevC++ 中就无法做到这一点。所以问题是使用这种方式或使用java而不是c++来解决的。

这是代码:

import java.util.Scanner; 

class kanji { 
    public static void main(String[] args) { 
         Scanner ob = new Scanner(System.in);  
         String kanji;
         System.out.println("\nWelcome to the Chinese letter program");
         System.out.println("This program will display one line with a Chinese letter.\n");
         System.out.println("Which is the Chinese letter?");
         kanji = ob.nextLine();
         System.out.println("\nThe Chinese letter is \"" + kanji + "\".\n");

    } //main
} //class

中文字母完全没有问题。

可以在终端用“javac -g kanji.java”编译,用“java kanji”执行。

于 2020-06-12T04:15:03.333 回答