2013-02-12 82 views
2

下面的代码:在wcin获取错误的字符输入?

#include <iostream> 

using std::wcin; 
using std::wcout; 
using std::locale; 


int main() 
{ 
    locale::global(locale("Portuguese_Brazil")); 

    wcout << "wcin Test using \"ção\": "; // shows that wcout works properly 
    wchar_t wcinTest[] = L""; 
    wcin >> wcinTest; 
    wcout << wcinTest << " should be \"ção\"."; 

    return 0; 
} 

结果:

wcin Test using "ção": ção 
╬Æo should be "ção". 

的╬字符是U + 2021或8225,且C是U + 00E7或231

我改变MULT字节选项,在项目属性中设置和不设置UNICODE。没有工作。

我已经将控制台字体设置为Consolas,它是一种能够正确显示ç字符的真正的字体。

我希望这可以作为将来的UNICODE控制台应用程序的标准做法,使其简单和可重复使用。

任何想法?

回答

1

这最后的工作:

#include <iostream> 
#include <string> 
#include <Windows.h> 

using std::cin; 
using std::cout; 
using std::string; 




int main() 
{ 
    SetConsoleOutputCP(1252); 
    SetConsoleCP(1252); 

    cout << "wcin Test using \"ção\": "; // shows that wcout works properly 
    string wcinTest; 
    cin >> wcinTest; 
    cout << wcinTest << " should be \"ção\"."; 

    return 0; 
} 

我太菜鸟明白,为什么我需要两个SetConsoleOutputCPSetConsoleCP。我虽然也许只是SetConsoleCP将修复一切,但不,我需要两个:SetConsoleOutputCP fixed cout;和SetConsoleCP fixed cin

还是要谢谢你@StoryTeller

+0

CP1252不是unicode。 – ssegvic 2016-02-05 10:36:02

+0

我同意,但它解决了我的具体问题,即使我不得不放弃utf8。更好,控制台只需要使用unicode字体 – orlando2bjr 2016-03-11 10:53:20

3

wcinTest是长度为1的wchar_t缓冲区;

当你读入它时,会溢出。使用一个std::wstring insead。

+0

尽管如此,没有好。与wstring相同的结果。 这是我的尝试: '\t的#include ' \t的#include使用std :: WCIN \t; \t使用std :: wcout; \t使用std :: wstring; \t使用std :: locale; \t // using namespace std; \t INT主() \t { \t \t区域设置::全球(区域( “Portuguese_Brazil”)); \t \t wcout <<“wcin使用\”ção\“进行测试:”; \t \t wstring wcinTest; \t \t wcin >> wcinTest; \t \t wcout << wcinTest <<“应该是”ção“。“; \t \t返回0。 \t}' – orlando2bjr 2013-02-12 14:41:59

+1

我觉得我到别的 我才意识到,我打破了我的CIN而固定我的语言环境COUT输出(” Portuguese_Brazil“)我删除和使用#include and everything works! – orlando2bjr 2013-02-12 18:04:46

+0

@ orlando2bjr,对你很好,伙伴:) – StoryTeller 2013-02-12 18:08:28

-2

这在VS 2012为我工作:

#include "stdafx.h" 
#include <io.h> 
#include <fcntl.h> 
#include <iostream> 
#include <string> 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    // don't use cin and cout after _setmode commands, 
    // it will cause runtime error 
    _setmode(_fileno(stdout),_O_U16TEXT); 
    _setmode(_fileno(stdin),_O_U16TEXT); 
    using namespace std; 

    wstring wstrTest; 
    wchar_t wcharTest, wcharTestOut; 
    int nTest; 

    wcout << L"Jaké je vaše křestní jméno? "; 
    getline(wcin, wstrTest); 
    wcin.get();  //remove EOL 

    wcout << L"Jakou známku si zasloužíte? "; 
    wcin >> wcharTest; 

    wcout << L"Kolik je vám let? "; 
    wcin >> nTest; 
    wcin.get(); 

    wcout << L"Jméno: " << wstrTest << endl; 
    wstrTest = L"Aleš"; //variable defined with wstring literal 
    wcout << L"Jméno: " << wstrTest << endl; 
    wcharTestOut = wcharTest + 1; 
    wcout << L"Známka: " << wcharTestOut << endl; 
    wcout << L"Věk: " << nTest << endl; 

    wcin.get(); //waiting for <Enter> before closing window 
    wcin.get(); 
    return 0; 
} 
+1

欢迎来到StackOverflow!您应该阅读本指南,以帮助您编写一个好的答案:http://stackoverflow.com/help/how-to-answer。错过的是解释你的回答并保持最短的工作解决方案。 – Bun 2014-08-19 19:12:54