Linux命令行下的C++程序输入汉字出问题,求解。
Ubuntu10.04,内核3.1.10,g++版本:g++ (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3
我写命令行下的程序,输入中文出错,我写了个测试程序如下:
#include <iostream>#include <string>#include <cstdio>#include <cstdlib>using namespace std;int main(){ string chinese; cout<<"输入中文:"; cin>>chinese; cout<<"你输入的是;"<<chinese<<endl; return 0;}
zym@zym-freedom:~/code$ ./chinese输入中文:abc123你输入的是;abc123zym@zym-freedom:~/code$ ./chinese输入中文:这是汉字你输入的是;这是汉字
zym@zym-freedom:~/code$ ./chinese输入中文:这是汉字 你输入的是;这是汉zym@zym-freedom:~/code$ ./chinese输入中文:这是汉字这是汉 你输入的是;这是汉字这是
#include <iostream>#include <string>#include <cstdio>#include <cstdlib>#include <locale.h>using namespace std;int main(){ string chinese; setlocale(LC_ALL,"chs"); cout<<"输入中文:"; cin>>chinese; cout<<"你输入的是;"<<chinese<<endl; return 0;}
[解决办法]
这个问题充分说明Linux终端的编写者的水平不如Windows终端cmd的编写者的水平高。(^_^)