首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请问下 wcout 为什么没输出?还有就是 wstring 里的东西为什么是乱码

2012-02-21 
请教下 wcout 为什么没输出?还有就是 wstring 里的东西为什么是乱码#include iostream#include string

请教下 wcout 为什么没输出?还有就是 wstring 里的东西为什么是乱码
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

void Test(wstring content)
{
size_t isize = content.size();
size_t ilength = content.length();

wcout<<"size: "<<isize<<" length: "<<ilength<<" "<<content;

return;
}

int main()
{
Test(L"哈哈");
cout<<endl<<"=============="<<endl;
wstring temp;
wcin>>temp;//也输入"哈哈",用这种方式 debug 调试时, content 是乱码,isize 和 ilength 也和上面结果不同,为什么?
Test(temp);//为什么不显示?
Sleep(1000);

}

[解决办法]



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

using namespace std; 

void Test(wstring content) 

size_t isize = content.size(); 
size_t ilength = content.length(); 

wcout<<"size: "<<isize<<" length: "<<ilength<<" "<<content; 

return; 


int main() 

std::wcout.imbue(std::locale("chs")); 
Test(L"哈哈"); 
cout<<endl<<"=============="<<endl; 
wstring temp; 
wcin>>temp; //也输入"哈哈",用这种方式 debug 调试时, content 是乱码,isize 和 ilength 也和上面结果不同,为什么? 
Test(temp); //为什么不显示? 
Sleep(1000); 

}
[解决办法]

C/C++ code
#include  <iostream > #include  <string > #include  <windows.h > using namespace std; void Test(wstring content) {     size_t isize = content.size();     size_t ilength = content.length();         wcout <<L"size: " <<isize <<L" length: " <<ilength <<L" " <<content; //宽字符输出,字串前都要加L        return; } int main() {     setlocale(LC_ALL,"chs");///这个不可少    Test(L"哈哈");     cout <<endl <<"==============" <<endl;     wstring temp;     wcin >>temp;      Test(temp); //为什么不显示?     Sleep(1000);     return 0;    } 

热点排行