【发布时间】:2015-03-31 14:55:00
【问题描述】:
我是新手,在调试代码方面需要帮助。 当我编译它'形式参数1的类型不完整'并且形式参数2的类型不完整'错误出现在
printf("Age is %d years.\n", calc_age(birth, current));
虽然'参数1('birth')的类型不完整'和'参数2('current')的类型不完整'错误出现在
int calc_age (struct date birth, struct date current) {
感谢您的帮助,谢谢!
#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;
}
【问题讨论】:
-
scanf()返回成功读取的项数:测试scanf()的返回值!如果它在程序结束之前没有打印东西......请记住stdout是缓冲的。在printf("...");之后添加fflush(stdout);,内容将在正确的时间显示。您可能希望在打印内容时添加换行符:在打印的字符串末尾添加\n。喜欢printf("...\n",...); -
struct date的定义移到calc_age的原型之前。 -
int year[1];等于int year;,如果不需要数组,请删除数组语法。