2013-02-12 124 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