回 帖 发 新 帖 刷新版面

主题:帮忙看下,错在哪?

/*  HELLO.C -- Hello, world */

#include "stdio.h"
#include "conio.h"
#include <stdlib.h> &iexcl;&iexcl;
#include <string.h>
#define N  3

struct student
 { long int number;
   char name[10];
   float s[5],sum,ave;
 }temp,stu[N]= {{10010,"lin",1,1,1,1,1},
                {10011,"huang",0,0,0,0,0},
                {10012,"cheng",2,2,2,2,2}
               };
void print(struct student stu[ ],int i)
 { int  j;
   printf("||number||    name||\n");
   printf("||%ld ||%8s||\n",stu[i].number,stu[i].name);
   for (j=0;j<5;j++)
   printf("score%d=:%8.1f\n",j+1,stu[i].s[j]);
   printf("\n");

  }
void Input(struct student stu[ ],int i)
 { int  j;
   float k;
     printf("Please input the student's number=:");
     scanf("%ld",&stu[i].number);
     printf("Please input the student's name=:");
     scanf("%s",&stu[i].name);
     for (j=0;j<5;j++)
        {printf("Please input the student's score%d=:",j+1);
         scanf("%f",&k);
         stu[i].s[j]=k;
        }
      print(stu,i);
  }


 void Add(struct student stu[ ],int i)
 { int  j;
   float k;
     printf("Please input the student's number=:");
     scanf("%ld",&stu[i+N-1].number);
     printf("Please input the student's name=:");
     scanf("%s",&stu[i+N-1].name);
     for (j=0;j<5;j++)
        {printf("Please input the student's score%d=:",j+1);
         scanf("%f",&k);
         stu[i+N-1].s[j]=k;
        }
      for(j=0;j<N+1;j++)
      {print(stu,j);}
  }

 void Change(struct student stu[ ],int i)
 {  int j,k;
   long int m;
   float p;
   printf("Please input the student's number:\n");
   scanf("%ld",&m);
   for(j=0;j<N;j++)
   {  if (stu[j].number==m)
      {  k=j;
       print(stu,j);
       printf("\n");
       }
   }
     do
    {printf("\t\t\t***  1.Change     the  student's score1     ***\t\t\t\n");
     printf("\t\t\t***  2.Change     the  student's score2     ***\t\t\t\n");
     printf("\t\t\t***  3.Change     the  student's score3     ***\t\t\t\n");
     printf("\t\t\t***  4.Change     the  student's score4     ***\t\t\t\n");
     printf("\t\t\t***  5.Change     the  student's score5     ***\t\t\t\n");
     printf("Please input i=:\n");
     scanf("%d",&i);
     switch (i)
      { case 1:printf("Please input the student's score1:\n");
              scanf("%f",&p);
              stu[k].s[0]=p;
              print(stu,k);break;
        case 2:printf("Please input the student's score2:\n");
              scanf("%f",&p);
              stu[k].s[1]=p;
              print(stu,k);break;
        case 3:printf("Please input the student's score3:\n");
              scanf("%f",&p);
              stu[k].s[2]=p;
           print(stu,k);break;
        case 4:printf("Please input the student's score4:\n");
              scanf("%f",&p);
              stu[k].s[3]=p;
             print(stu,k);break;
        case 5:printf("Please input the student's score5:\n");
              scanf("%f",&p);
              stu[k].s[4]=p;
              print(stu,k);break;
      }}while (i!=0);
      printf("please wait input any key continue");
      getch();
    }
 void Delete (struct student stu[ ],int i)
 { long int m;
   printf("Please input the student's number=:\n");
   scanf("%ld",&m);
   for(i=0;i<N;i++)
   {  if (stu[i].number==m)
      {  stu[i]=stu[i+1];
       }
    }
    for(i=0;i<N-1;i++)
    { print(stu,i);
     }
 }
 void Inquire(struct student stu[ ],int i)
 { long int m;
   printf("Please input the student's number=:\n");
   scanf("%ld",&m);
   for(i=0;i<N;i++)
   {  if (stu[i].number==m)
      { print(stu,i);
       }
   }
   printf("please wait input any key continue");
   getch();
}
  void Sort(struct student stu[ ],int i)
  {int j;
   float a;
    for (i=0;i<N;i++)
     {for (j=0;j<5;j++)
       {a=0;
        a+=stu[i].s[j];
        stu[i].sum=a;
        print(stu,i);
        printf("%8.1f\n",stu[i].sum);
        }
      }
     for (i=0;i<N;i++)
      {for (j=0;j<N+1;j++)
        {if (stu[i].sum>stu[j].sum)
           {temp=stu[i];
            stu[i]=stu[j];
            stu[j]=temp;
            }
        }
        print(stu,i);
        printf("%8.1f\n",stu[i].sum);
       }
       printf("please wait input any key continue");
       getch();
   }
 void Statistics(struct student stu[ ],int i)
 {int j,m=0,b=0;
   for (i=0;i<3;i++)
     for (j=0;j<5;j++)
       {if (stu[i].s[j]<60)
          { m++;
            printf("||number||name||\n");
            printf("||%ld||%s||\n",stu[i].number,stu[i].name);
           }
       }
       printf("the student don't pass exam have:%d\n",m);
       for (i=0;i<3;i++)
           for (j=0;j<5;j++)
           {  if (stu[i].s[j]>80)
               {b++;
                printf("||number||name||\n");
                printf("||%ld||%s||\n",stu[i].number,stu[i].name);
               }
           }
           printf("the student  pass exam have:%d\n",b);
}



main()
 {  int select,i=0;
    float k;
    do
    {
    printf("\t\t\t***--------------------------------------------***\t\t\t\n");
    printf("\t\t\t***                                            ***\t\t\t\n");
    printf("\t\t\t***  Welcome to The Student's Mangement System ***\t\t\t\n");
    printf("\t\t\t***                                            ***\t\t\t\n");
    printf("\t\t\t***--------------------------------------------***\t\t\t\n");
    printf("\t\t\t***  1.Input      the  student's date          ***\t\t\t\n");
    printf("\t\t\t***  2.Add        the  student's date          ***\t\t\t\n");
    printf("\t\t\t***  3.Delete     the  student's date          ***\t\t\t\n");
    printf("\t\t\t***  4.Change     the  student's date          ***\t\t\t\n");
    printf("\t\t\t***  5.Inquire    the  student's date          ***\t\t\t\n");
    printf("\t\t\t***  6.Sort       the  student's date          ***\t\t\t\n");
    printf("\t\t\t***  7.Statistics the  student's date          ***\t\t\t\n");
    printf("\t\t\t***  0.Escape     the  student's system        ***\t\t\t\n");
    printf("Please input select=:\n");
    scanf("%d",&select);
      switch (select)
      {  case 1:Input(stu,i);break;
         case 2:Add(stu,i);break;
         case 3:Delete(stu,i);break;
         case 4:Change(stu,i);break;
         case 5:Inquire(stu,i);break;
         case 6:Sort(stu,i);break;
         case 7:Statistics(stu,i);break;
         default:printf("It's wrong");break;
       }
    }while(select!=0);
 }

回复列表 (共3个回复)

沙发

里面有一处比较明显的错误:
  Input Add函数中,scanf("%s",&stu[i].name); name成员本来就是一个数组(类型为char[10]),再求地址类型将变成char (*)[10];应该把取地址符去掉。

还有 stu[N]= {{10010,"lin",1,1,1,1,1},这样的初始化  以及 main() 用gcc编译肯定会有警告。
最好写成stu[N]= {{10010,"lin",{1,1,1,1,1}},            int main(void)

板凳


可是我运行的时候,错误的是输入数据后便自动关掉,没有总体输出数据

3 楼

[quote]
E:\c>gcc -Wall -Werror -std=c99  foo.c -o foo.exe
cc1.exe: warnings being treated as errors
foo.c:12:2: error: missing braces around initializer
foo.c:12:2: error: (near initialization for 'stu[0].s')
foo.c: In function 'Input':
foo.c:31:6: error: format '%s' expects type 'char *', but argument 2 has type 'char (*)[10]'
foo.c: In function 'Add':
foo.c:47:6: error: format '%s' expects type 'char *', but argument 2 has type 'char (*)[10]'
foo.c: At top level:
foo.c:178:1: error: return type defaults to 'int'
foo.c: In function 'main':
foo.c:180:11: error: unused variable 'k'[/quote]


按照上面的警告或错误,修改源代码吧、

我来回复

您尚未登录,请登录后再回复。点此登录或注册