繁体   English   中英

功能类型冲突?

[英]Conflicting Types for Function?

我有一个程序可以在结构数组中接收有关5个学生的信息,然后使用该信息进行计算以确定学费。 目前,我不知所措,无法解决这些错误。 例如,它说

#include <stdio.h>
#include <stdlib.h>
#define UnitCost 100
#define MAX 60
int calculator(int x, char y);
void getInput(struct student* memb);
void printOutput(struct student * memb);

typedef struct student{
    char name[MAX];
    char housing;
    int total;
    int units;
    } student;
int main()
{
    struct student user[5];
    int total[5] = {0,0,0,0,0};
    int loopControl;
    int i;
    char name[5][MAX];
    int units;
    char housing;
    for (loopControl = 0; loopControl <= 4; loopControl++){

        getInput(&user);
        total[loopControl] = calculator(units, housing);


    }
    for(i = 0; i < 5; i++){
    printf("\nStudent Name: %s", user[i].name);
    printf("\nAmount due: $%d\n", user[i].total);
    }
    printf("\nAverage: %d\n", (total[0] + total[1] + total[2] + total[3] + total[4])/5);
    return 0;
}
// -----------------------------------------------------------------------------
int calculator(int x, char y){
    int onCampusCost = 0;
    int unitCostTotal = 0;
    int unitsEnrolledDiscount = 0;
    if (x > 12){
           unitsEnrolledDiscount = (x - 12) * 10;
        }
    if (y == 'n'){
        onCampusCost = 0;
        }
        else if (y == 'y'){
        (onCampusCost = 1000);
        }
    if (x >12){
        unitCostTotal = (x * 100) - ((x - 12) * 10);
    }
    else{
        unitCostTotal = x * 100;
    }

    return onCampusCost + unitCostTotal;

}

void printOutput(struct student * memb){
}
void getInput(struct student* memb){
    char name[5][MAX];
    char house;
    int uns;
    printf("Enter student name: ");
    if (iter > 0) getchar();
    gets(name);
    memb->name = name;
    printf("Do you live on campus?\ny/n: ");
    scanf("%c", house);
    memb->housing = house;

    printf("How many units are you enrolled in?: ");
    scanf("%d", uns);
    memb->units = uns;
}

错误:

|| ===构建:在f中调试(编译器:GNU GCC编译器)=== | 警告:“结构学生”在参数列表中声明| 警告:“结构学生”在参数列表中声明|

|在函数“ main”中:|

警告:从不兼容的指针类型传递'getInput'的参数1

注意:预期为“结构学生”,但参数的类型为“结构学生( )[5]” |

警告:未使用的变量“名称” [-Wunused-variable] |

在函数“计算器”中:| 警告:设置了变量'unitsEnrolledDiscount'但未使用[-Wunused-but-set-variable] |

错误:“ printOutput”的类型冲突

注意:'printOutput'的先前声明在此处|

错误:“ getInput”的类型冲突|

注意:“ getInput”的先前声明在此处|

在函数“ getInput”中:|

错误:未声明“ iter”(此函数中的首次使用)|

注意:对于每个未声明的标识符,对于每个出现在||中的函数,只报告一次。

警告:从不兼容的指针类型|中传递'gets'参数1

注意:预期为'char ',但参数的类型为'char( )[60]'|

错误:分配给具有数组类型|的表达式

警告:格式'%c'期望的参数类型为'char *',但参数2的类型为'int'[-Wformat =] |

警告:格式'%d'期望参数类型为'int *',但是参数2的类型类型为'int'[-Wformat =] | || ===构建失败:4个错误,9个警告(0分钟,0秒)=== |

请请帮助

@Lumii问题出在您的代码中:-

  1. 您已经定义了函数声明,在该函数声明中,在结构定义之前传递了(struct student *)作为参数。 因为struct是用户定义的数据类型,这就是为什么编译器给您错误的原因,因为编译器没有得到struct的定义。 因此,要么在结构定义之后编写所有函数声明,要么在函数声明之前编写结构名称。 喜欢:-

指导学生

int计算器(int x,char y);

无效的getInput(结构学生*成员);

无效的printOutput(结构学生*成员);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM