c语言输入不定长数组,第一个数总是消失,程序如下,请大牛回答,怎么改

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
int _tmain(int argc, _TCHAR* argv[])
{
int a[100],i,j;
char c;
i=0 ;
while((getchar())!='\n')
{
scanf("%d",&a[i]);
i++;
}
for(j=0;j<i;j++)
printf("%d\n",a[j]);
getchar();
return 0;
}
比如输入1 2 3
输出 2 3

输入的时候 在前面加一个空格 因为被你的getchar()把1拿走了
或者代码改为
do
{
scanf("%d",&a[i]);
i++;
}while((getchar())!='\n');
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-04-21
while((c=getchar())!='\n')
{
a[i]=c;
i++;
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网