利用getchar函数和ungetc函数,通过getchar函数判断回车,以及判断时候为数字,然后通过ungetc函数,将通过getchar函数获得的字符送回缓冲区,再通过cin函数取出作为int型数组。
程序代码:#include<iostream>
using namespace std;
int main()
{
int a[50];
int i = 0;
char c;
while((c=getchar())!=‘\n‘)
{
if(c>=‘0‘&&c<=‘9‘)
{
ungetc(c,stdin);
cin>>a[i++];
}
}
for(int j=0;j<i;j++)
{
cout<<"a["<<j<<"]:"<<a[j]<<endl;
}
}
原文:http://www.cnblogs.com/8335IT/p/7745013.html