请任何人查看我的代码,看看它有什么问题

时间:2014-10-02 04:48:10

标签: c

#include <stdio.h>

int getAvg(int a, int b, int c);
int main()
{
  int a,b,c;
  int i;
  int avg[5];

  char name[5][10] ;
  int korean[5], english[5], maths[5] ;
  char message[2][10] = {"Pass","No Pass"};

  for ( i = 0; i < 5; i++ )
  {
      printf("Enter your marks <name,korean,english,maths \n");
      scanf("%s%d%d%d",name[i],&korean[i],&english[i],&maths[i]);
  }


  for (i = 0; i < 5; i++)
 {


    printf("name:%s, korean:%d, english:%d, maths:%d, average:%d",name[i],korean[i],english[i],maths[i],avg[i]);
    avg[i] = getAvg(a,b,c);

    if (avg[i]>60)
    {
      printf("==%s",message[0]);
    }

    else
    {
      printf("==%s",message[1]);
    }

 }


int getAvg(int a, int b, int c)
  {
    int avg;

    avg = (a+b+c)/3;

    return avg;
  }
 }

我想像这样打印

Enter your marks <name,korean,english,maths>
kim 10 50 60
Enter your marks <name,korean,english,maths>
hanna 50 60 70
Enter your marks <name,korean,english,maths>
lee 80 70 60
Enter your marks <name,korean,english,maths>
lori 70 80 90
Enter your marks <name,korean,english,maths>
kang 60 70 80

name:kim,korean:10,english:50,maths:60,average:40 == no pass
name:hanna,korean:50,english:60,maths:70,average:60 == no pass
name:lee,korean:80,english:70,maths:60,average:70 == pass
name:lori,korean:70,english:80,maths:90,average:80 == pass
name:kang,korean:60,english:70,maths:80,average:70 == pass

我真的很抱歉,如果事实证明这是我的错误或问题太初级了......它应该在5个小时内到期,我无法弄清楚出了什么问题......它一直告诉我getAve函数是未定义的参考,我认为它没有任何问题..请有人帮助我吗? :(

4 个答案:

答案 0 :(得分:1)

getAvg位于main内。将其移出(或者等效地,将一个外部大括号从代码末尾移动到getAvg定义开始之上)。计算平均值也有问题,但这是一个逻辑错误,而不是语法错误。 (具体来说,您正在计算abc的平均值 - 检查您在哪里定义这些变量的值,并在您之前重新打印内容计算它。)

答案 1 :(得分:1)

你得到“对getAvg函数的未定义引用”,因为你在定义main函数之前没有关闭getAvg。只需在函数定义之前移动最后一个},该错误就应该消失了!

然后,你调用这个函数

avg[i] = getAvg(a,b,c);

abc未初始化!所以你需要

a=korean[i];
b=english[i];
c=maths[i];

之前。您还需要在计算之前打印avg[i]。因此,在函数调用后移动printf以获得所需的结果。

答案 2 :(得分:0)

固定支架定位后,这里也存在问题:

printf("name:%s, korean:%d, english:%d, maths:%d, average:%d",name[i],korean[i],english[i],maths[i],avg[i]);
avg[i] = getAvg(a,b,c);

您必须在显示之前计算平均值。此外,a, b, c是未初始化的变量。你可能意味着:

avg[i] = getAvg( korean[i], english[i], maths[i] );
printf("name:%s, korean:%d, english:%d, maths:%d, average:%d",
        name[i], korean[i], english[i], maths[i], avg[i]);

答案 3 :(得分:0)

首先将getAvg(...)放在main()

之外

现在在打印getAvg(...)

之前致电avg[i];
avg[i] = getAvg(korean[i],english[i],maths[i]);
printf("name:%s, korean:%d, english:%d, maths:%d, average:%d",name[i],korean[i],english[i],maths[i],avg[i]);