How to display lines with Chinese letters on DevC++?

833 Views Asked by At

What I want is to display a line with a Chinese letter, entered by the user. Though the program DevC++.

This is my code:

#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;
}

What matters to me is the "The Chinese letter is "(kanji)"." line. When I do what the program says I get "The Chinese letter is "?".". So the problem is that DevC++ doesn't show the Chinese letters correctly, even when I using the wcin and wcout things.

Note I am using DevC++ on Ubuntu, through wine.

3

There are 3 best solutions below

2
Oving Cheng On

Do your console support Chinese encoding? take a look at this post How to print a Chinese character?

0
Ĥvan_Eo On

The problem was solved using html to complement what DevC++ cannot do, which is working with Chinese letters.

The c++ code ended up like this:

#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;
}

Which generates an html code that can display Chinese letters. For example like this:

<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>

Ending with what was expected from the c++ program:

The Chinese letter is "銀".

The Chinese letter is "囗". 
0
Ĥvan_Eo On

That cannot be done in DevC++ without using jury-rigged tricks. So the problem was solved using that way or using java instead of c++.

Here is the code:

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

Having no problems at all with the Chinese letters.

It can be compiled in the terminal with "javac -g kanji.java", and executed with "java kanji".