2016-12-03 2 views
0

У меня есть программа, которая принимает информацию о 5 учениках в массиве struct, а затем выполняет расчет с использованием этой информации для определения стоимости обучения. Прямо сейчас, я перегружен ошибками, которые я не знаю, как исправить. Например, он говорит, чтоКонфликтующие типы для функции?

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

Ошибки:

||=== 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)) ===|

Пожалуйста, пожалуйста, пожалуйста, помогите

+0

Проведите время до _really_ _read_ журнала ошибок/предупреждений. Например, 'getInput (&user);' очевидно, что вы передаете неправильный тип, измените '& user' на' user [loopControl] ' – artm

+0

@artm:' & user [loopControl] ' –

+1

В C вы читаете первое предупреждение/ошибку , исправьте его, а затем повторите попытку. В вашем случае первое предупреждение говорит о том, что вы указали 'struct student' внутри списка параметров. Конечно же, первый раз, когда« struct student »появляется в вашем коде, находится в прототипе функции для' getInput 'Так что переместите прототипы функций ниже определения структуры и повторите попытку. – user3386109

ответ

0

@Lumii Проблема была в вашем коде: -

  1. вы определили которое вы передали (struct student *) в качестве аргумента перед определением структуры. Поскольку struct - это пользовательский тип данных, поэтому компилятор дает вам ошибку, потому что компилятор не получил определение struct. Поэтому либо напишите объявление функции после определения структуры, либо запишите имя структуры перед объявлением функции. :

struct student;

int calculator (int x, char y);

void getInput (struct student * memb);

void printOutput (struct student * memb);

Смежные вопросы