简体   繁体   中英

Conflicting Types for Function?

I have a program that takes in information about 5 students in a struct array, then does a calculation using that information to determine a tuition cost. Right now, I'm getting overloaded with errors that I don't know how to fix. For example, it says that

#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;
}

The errors:

||=== Build: Debug in f (compiler: GNU GCC Compiler) ===| warning: 'struct student' declared inside parameter list| warning: 'struct student' declared inside parameter list|

|In function 'main':|

warning: passing argument 1 of 'getInput' from incompatible pointer type|

note: expected 'struct student ' but argument is of type 'struct student ( )[5]'|

warning: unused variable 'name' [-Wunused-variable]|

In function 'calculator':| warning: variable 'unitsEnrolledDiscount' set but not used [-Wunused-but-set-variable]|

error: conflicting types for 'printOutput'|

note: previous declaration of 'printOutput' was here|

error: conflicting types for 'getInput'|

note: previous declaration of 'getInput' was here|

In function 'getInput':|

error: 'iter' undeclared (first use in this function)|

note: each undeclared identifier is reported only once for each function it appears in|

warning: passing argument 1 of 'gets' from incompatible pointer type|

note: expected 'char ' but argument is of type 'char ( )[60]'|

error: assignment to expression with array type|

warning: format '%c' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]|

warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]| ||=== Build failed: 4 error(s), 9 warning(s) (0 minute(s), 0 second(s)) ===|

Please please please help

@Lumii Problem was in your code :-

  1. you have defined function declaration in which you passed (struct student *) as a argument before the structure definition. Because struct is user defined data type that's why compiler giving you error because compiler has not got the definition of struct. So either write all function declaration after the struct definition or write struct name before the function declaration. like:-

struct student;

int calculator(int x, char y);

void getInput(struct student* memb);

void printOutput(struct student * memb);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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