範例一:cin.get(array,10);輸入abc 按enter鍵兩次.
(若為cin.getline(array,10);輸入abc後須按三次enter鍵,第一次enter鍵將被捨去,第二次enter鍵則給cin.get(ch1);接收
第三次enter鍵則給 cin.get(ch2);
輸出:
array[0] =a
ch2= (第二次enter鍵的空字元)
(int)ch2=10 (第二次enter鍵空字元的10進制值10)
int main()
{
char ch1,ch2,array[10];
cin.get(array,10);
cin.get(ch1);
cin.get(ch2);
cout<<"array[0] = "<<array[0]<<endl;
cout<<"array[1] = "<<array[1]<<endl;
cout<<"array[2] = "<<array[2]<<endl;
cout<<"array[3] = "<<array[3]<<endl;
cout<<"(int)array[3] = "<<(int)array[3]<<endl;
cout<<"array[4] = "<<array[4]<<endl;
cout<<"(int)array[4] = "<<(int)array[4]<<endl;
cout<<"ch1 = "<<ch1<<endl;
cout<<"(int)ch1 = "<<(int)ch1<<endl;
cout<<"ch2 = "<<ch2<<endl;
cout<<"(int)ch2 = "<<(int)ch2<<endl;
範例二:輸入abcp 按enter
cin.get(array,10,'p');的停止擷取字元p留在buffer,被第二次cin.get(ch1);函式擷取輸入到ch1
enter鍵字元被第三次cin.get(ch2);函式擷取輸入到ch2
輸出:
array[0] =a
(int)ch1=112 (cin.get(array,10,'p');的停止擷取字元p的10進制值)
ch2= (按enter鍵空字元)
(int)ch2=10 (cin.get(array,10,'p');按enter鍵空字元的10進制值10)
int main()
{
char ch1,ch2,array[10];
cin.get(array,10,'p');
cin.get(ch1);
cin.get(ch2);
cout<<"array[0] = "<<(int)array[0]<<endl;
cout<<"array[1] = "<<(int)array[1]<<endl;
cout<<"array[2] = "<<(int)array[2]<<endl;
cout<<"array[3] = "<<(int)array[3]<<endl;
cout<<"array[4] = "<<(int)array[4]<<endl;
cout<<"array[5] = "<<(int)array[5]<<endl;
cout<<"ch1 = "<< ch1<<endl;
cout<<"ch1 = "<<(int)ch1<<endl;
cout<<"ch2 = "<< ch2<<endl;
cout<<"ch2 = "<<(int)ch2<<endl;
system("pause");
return 0;
}