c - 运行时错误: Structure for student details in C

标签 c runtime-error

这是一个使用结构来提供和显示学生详细信息的 C 程序。运行程序时会出现逻辑错误。我找不到原因。请帮忙,如果可能的话发布正确的代码。

编辑:我之前遇到了段错误,然后我根据这篇文章中的评论之一(@CiaPan)编辑了代码,发现运行代码时仍然存在逻辑错误。

      #include <stdio.h>

      struct student
      {
      char name[100];
      int roll,mark[100];
      float average;
      }a[100];

      int main()

      {
      int n,i,j,m,sum;
      printf("enter the no. of students and no. of subjects : \n");
      scanf("\n%d\n%d",&n,&m); 
      printf("enter the student details : \n");
      for(i=0;i<n;i++)

      {   
      printf("\n%d",i+1);
      printf("\nenter the name :");
      scanf("%s",a[i].name);
      printf("\nenter the roll no.");
      scanf("%d",&a[i].roll);
      printf("\nmarks :\n");
      for(j=0;j<m;j++)
      {
      printf("%d.\t",j+1);
      scanf("%d\n",&a[i].mark[j]);
      }

      }    
      for(i=0;i<n;i++)
      { 
      sum=0;
      for(j=0;j<m;j++)
      sum=sum+a[i].mark[j];
           }
      for(i=0;i<n;i++) 
      a[i].average=sum/m;

      for(i=0;i<n;i++)
      if(a[i].average>=75)
      printf("\n%s\n%d\n",a[i].name,a[i].roll);
      return 0;

      }

我解决了这个问题。

Edit2:我在输出中遇到一些错误。用于获取标记的 scanf 函数中有一个错误,我已按所示更正了该错误。由于与 for 循环相关的逻辑错误,计算平均值也存在问题。

 //Correct code

 #include <stdio.h>

      struct student
      {
      char name[100];
      int roll,mark[100];
      float average;
      }a[100];

      int main()

      {
      int n,i,j,m,sum;
      printf("enter the no. of students and no. of subjects : \n");
      scanf("\n%d\n%d",&n,&m); 
      printf("enter the student details : \n");
      for(i=0;i<n;i++)

      {   
      printf("\n%d",i+1);
      printf("\nenter the name :");
      scanf("%s",a[i].name);
      printf("\nenter the roll no.");
      scanf("%d",&a[i].roll);
      printf("\nmarks :\n");
      for(j=0;j<m;j++)
      {
      printf("%d.\t",j+1);
      scanf("\n%d",&a[i].mark[j]);
      }

      }   



      for(i=0;i<n;i++)
      { 
        sum=0;
      for(j=0;j<m;j++)
        sum=sum+a[i].mark[j];



      //for(i=0;i<n;i++) 
      a[i].average=sum/m;
      }


      for(i=0;i<n;i++)
      if(a[i].average>=75)
      printf("\n%s\n%d\n",a[i].name,a[i].roll);
      return 0;

      }

最佳答案

scanf 函数需要知道在哪里存储它输入的数据。为此,它需要获取指向变量的指针来接收数据。因此,您必须传递指向 int 变量的指针,而不是这些变量的值:

    scanf( "%d%d", &n, &m );

而不是

    scanf( "%d%d", n, m );

与输入标记相同。

另一方面,数组的名称会衰减为指向数组第一项的指针,因此当您需要输入字符串时,应该传递数组变量:

    scanf( "%s...", a[i].name, ... );

而不是

    scanf( "%s...", &a[i].name, ... );

因为&a[i].name是一个指向数组的指针,而您需要一个指向数组第一个字符的指针在这里。

此外,在对分数求和时,您应该将每个学生的总和重置为零。否则,对于除第一个学生之外的每个学生,您都会累积之前所有学生的分数。

另外,您应该测试 scanf 的返回值,以确保输入数据已成功读取,并可能测试它们的一致性(例如,您的程序将接受负标记...)。

关于c - 运行时错误: Structure for student details in C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50578928/

相关文章:

c - 为什么最大堆栈深度不断变化?

c - 对于 C 中失败的函数调用,您应该使用什么返回值?

python - (错误)[ERROR] : Connection lost before response written

c++ - 初始化一个变量

c++ - LD_PRELOAD 导致 linux 命令的段错误

java - 捕获运行时错误 Java

c# - Affectiva C# SDK 崩溃

java - 为什么我的程序在文件存在时捕获/抛出 FileNotFoundException?

c - 无法重命名匿名声明 - Xcode 7.1.1

android - java.lang.ArithmeticException : divide by zero in onActivityResult 异常