今天用许久未碰的C语言写数据结构作业时遇到了这个问题,在不确定要输入的整型数组长度时我们应该如何输入,并用回车结束输入?如:LA=(3,5,8,11),LB=(2,6,8,9,11,15,20)
在这给出两种解决办法
int count=0;
int a[1024]={0};
while(1)
{
scanf("%d",&a[count]);
count++;
if(getchar() == 'n')//遇到换行时跳出循环
{
break;
}
}
for(temp=0;temp
如果你不希望在while循环中再进行判断,那么可以使用do while
int count=0;//count用于计数
int a[1024]={0};
do//注意,不能直接使用while(getchar!='n),而应该使用do while,
//因为前者会导致第一个数据丢失(程序运行先进入判断条件第一个数字被getchar拿走)
{
scanf("%d",&a[count]);
count++;
}while(getchar()!='n');//遇到换行时结束循环
for(temp=0;temp
以上也只是我临时查阅资料,经过调试之后总结出来的,限于知识水平难免有错漏之处,请多指正包涵。



