gpt4 book ai didi

C代码错误不是函数或函数指针

转载 作者:太空宇宙 更新时间:2023-11-04 01:17:45 24 4
gpt4 key购买 nike

我有以下 C 代码。当我尝试编译它时,我收到一条错误消息:

called object 'countPassed' is not a function or function pointer"

我该如何解决这个问题?

#include <stdio.h>

int main()
{
int countWhoPassed, averageCalculate, printPassed;
int amount = howManygrades();
storesGrades(amount);
printAll(amount);
averageCalculate = averageCalc(amount);
printAverage(amount);
countWhoPassed = countPassed(amount);
printPassed = printWhoPassed(amount);
return 0;
}

int howManygrades()
{
int gradesAmount;
printf("How many grades do you want?\n");
scanf("%d", &gradesAmount);
while (gradesAmount > 10) {
printf("Sorry, this amount of numbers is too big for the array.");
scanf("%d", &gradesAmount);
while (gradesAmount < 0) {
printf("Sorry, this amount of numbers is too small for the array.");
scanf("%d", &gradesAmount);
}
}
while (gradesAmount < 0) {
printf("Sorry, this amount of numbers is too small for the array.");
scanf("%d", &gradesAmount);
while (gradesAmount > 10) {
printf("Sorry, this amount of numbers is too big for the array.");
scanf("%d", &gradesAmount);
}
}

return gradesAmount;
}

void storesGrades(int amount)
{
int iterator;
float grade;
for (iterator = 0; iterator < amount; iterator++)
{

printf ("Enter another grade.");
scanf("%f", &grade);
while ((grade <= 0.0) || (grade > 10.0))
{
printf("Sorry, this number is not valid.");
scanf("%f", &grade);
}
grades[iterator] = grade;
}
}

void printAll(int amount)
{
int iterator;
for (iterator = 0; iterator < amount; iterator++)
{
printf("%.1f\n", grades[iterator]);
}
}

int averageCalc(int amount)
{
float sum, averageResult, average;
int iterator;
sum = 0.0;
for (iterator = 0; iterator < amount; iterator++) {
sum += grades[iterator];
}
average = sum / (float)amount;
return average;
}

void printAverage(int amount)
{
float printThisAverage = averageCalc(amount);
printf("%.2f", printThisAverage);
}

int countPassed(int amount)
{
int iterator, countPassing;
countPassing = 0;
for (iterator = 0; iterator < amount; iterator++)
{
if (grades[iterator] >= 5.5)
{
countPassing++;
}
}
return countPassing;
}

int printWhoPassed(int amount)
{
int returnValue = countPassed(amount);
return returnValue;
}

最佳答案

似乎有几个问题。我认为您收到的错误消息很奇怪,它应该说 countPassed 未定义。那是因为你在调用它之后定义它。您可以通过在调用函数之前添加函数原型(prototype)来修复它。通过这种方式,您可以告诉编译器函数的存在(还没有定义函数)并且它会编译。对我来说,它的工作原理是将以下原型(prototype)添加到您的代码中,放在主要部分之前:

int countPassed(int amount);
void printAll(int amount);
void printAverage(int amount);
void storesGrades(int amount);

或者,您可以将 main 函数放在所有其他函数之后。当然,这在实际项目中并不总是一个选项。

此外,grades 没有定义。我假设您想要一个 int 数组,所以添加类似 int grades[10] 的东西应该可以解决问题(在这个例子中,最多可以使用 10 个等级,它也可能动态分配该内存以允许用户想要的任何数量)。

此外,我还必须添加 #include "stdio.h" 以使其与 printf 一起编译,尽管有些编译器不会介意它是否丢失。

关于C代码错误不是函数或函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53410977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com