#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"的话,读入数据会发生错误:如图C语言scanf的一些问题
如果把最后的"\n"去掉以后,发现输入的正确的!但是不知道其中的原理是为什么,先记录下来。等解决以后再补充完整。

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

相关文章:

  • 2021-04-01
  • 2021-04-30
  • 2021-09-22
  • 2022-12-23
  • 2022-02-06
  • 2021-11-28
  • 2020-04-13
  • 2022-02-05
猜你喜欢
  • 2021-07-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-16
  • 2021-10-22
  • 2022-12-23
相关资源
相似解决方案