0

I'm a newbie and I need help with debugging my code. When I compile it 'type of formal parameter 1 is incomplete' and type of formal parameter 2 is incomplete' error appears in

 printf("Age is %d years.\n", calc_age(birth, current));

while 'parameter 1 ('birth') has incomplete type' and ' parameter 2 ('current') has incomplete type' errors appear in

int calc_age (struct date birth, struct date current) {

Help is appreciated, thanks!

#include <stdio.h>
int calc_age (struct date birth, struct date current);


int main(void)
{

    struct date {
        int month[1];
        int day[1];
        int year[1];

};


    struct date birth, current;
    char c;

    printf("Input the birthdate (MM/DD/YY): ");
    scanf("%d %c %d %c %d", birth.month, &c, birth.day, &c, birth.year);
    printf("Input date to calculate (MM/DD/YY): ");
    scanf("%d %c %d %c %d", current.month, &c,  current.day, &c, current.year);

    printf("Age is %d years.\n", calc_age(birth, current));

    return 0;

}

int calc_age (struct date birth, struct date current) {

    int age;

    if (birth.month[0] < current.month[0]) {
        age = (current.year[0] - birth.year[0]);
    } else if (birth.month[0] > current.month[0]) {
        age = (current.year[0] - birth.year[0] - 1);
    } else {
        if (birth.day[0] <= current.day[0]) {
            age = (current.year[0] - birth.year[0]);
        } else {
            age = (current.year[0] - birth.year[0] - 1);
        }
    }

    return age;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
eternum
  • 21
  • 1
  • 1
  • 3
  • `scanf()` returns the number of items successfully read : test the return value of `scanf()` ! If it does not print things before the end of the program...Remember that stdout is buffered. Add `fflush(stdout);` after `printf("...");` and things wil be displayed at the right time. You may want to add newline as things are printed : add `\n` at the end of the string printed. Like `printf("...\n",...);` – francis Mar 31 '15 at 15:01
  • 3
    define of `struct date` move to before prototype of `calc_age`. – BLUEPIXY Mar 31 '15 at 15:03
  • 2
    `int year[1];` is equal to just `int year;`, drop the array syntax if you don't need arrays. – Alex Díaz Mar 31 '15 at 15:05

2 Answers2

7

Your program is showing incomplete type error because the scope of struct date is limited to main() function only. Outside the main() the structure definition is not visible.

So, the struct date definition should be in global scope so that it is visible from calc_age() (and maybe other functions, too). Even better, if you can create and maintain a header file for this purpose.

That said, in your code, as per your current requirement, get rid of single-element arrays in the structure, like

struct date {
    int month;
    int day;
    int year;
 };

and also, the scanf() statement

scanf("%d %c %d %c %d", birth.month, &c, birth.day, &c, birth.year);

should read

scanf("%d %c %d %c %d", &birth.month, &c, &birth.day, &c, &birth.year);
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    It's a good point to advice a modification of struct date. Having arrays of size 1 as fields is unusual. I don't know where such a feature would be useful. And function `calc_age()` would be more elegant. – francis Mar 31 '15 at 15:14
3
#include <stdio.h>
struct date {
    int month[1];
    int day[1];
    int year[1];

};

int calc_age (struct date birth, struct date current);


int main(void)
{
struct date birth, current;
char c;

printf("Input the birthdate (MM/DD/YY): ");
scanf("%d %c %d %c %d", birth.month, &c, birth.day, &c, birth.year);
printf("Input date to calculate (MM/DD/YY): ");
scanf("%d %c %d %c %d", current.month, &c,  current.day, &c, current.year);

printf("Age is %d years.\n", calc_age(birth, current));

return 0;

}

int calc_age (struct date birth, struct date current) {
int age;

if (birth.month[0] < current.month[0]) {
    age = (current.year[0] - birth.year[0]);
} else if (birth.month[0] > current.month[0]) {
    age = (current.year[0] - birth.year[0] - 1);
} else {
    if (birth.day[0] <= current.day[0]) {
        age = (current.year[0] - birth.year[0]);
    } else {
        age = (current.year[0] - birth.year[0] - 1);
    }
}

return age;
}

You should define the struct before main

icecity96
  • 1,177
  • 12
  • 26