C语言scanf的一些问题

#include<stdio.h>
int Readscore(int score[],int id[]);
int Findmax(int m,int score[]);
int main()
{
    int score[40],id[40];
    int n,m;
    n=Readscore(score,id);
    m=Findmax(n,score);
    printf("the max number is %d, the id  is %d",score[m],id[m]);
    return 0;
}
int Readscore(int score[],int id[])
{
    int i,n;
    for(i=0;;i++)
    {
     printf("input score:");
     scanf("%d",&score[i]);
     printf("input id:");
     scanf("%d\n",&id[i]);

       if(score[i]<0)
           break;
    }
           n=i;
    printf("the total number is %d\n",n);
    return n;
}
int Findmax(int m, int score[])
{
    int q,j,max;
    q=0;
    max=score[0];
    for(j=1;j<=m-1;j++)
    {
          if(score[j]>max)
          {
            max=score[j];
              q=j;
          }
    }
        return q;
}

红色部分如果加上"\n"的话,读入数据会发生错误:如图
如果把最后的"\n"去掉以后,发现输入的正确的!但是不知道其中的原理是为什么,先记录下来。等解决以后再补充完整。

经过百度查询,发现在scanf中加入"\n",得需要(整数、浮点数或者字符型)来结束输入,通过输入回车来结束输入是不正确的,控制输入结束的(整数、浮点数或者字符型)先存在缓存区中,会被下一次scanf读取。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值