我在编写C程序时遇到了一个未知错误

时间:2013-12-04 21:44:18

标签: c error-handling

继承我的代码:

#include <stdio.h>

main()
{
int yearcounter = 0;
int monthcounter = 0;
int monthcounter2 = 0;
int extramonths = 0;
int monthsalive = 0;
int month;
int day;
int year;
int monthto;
int dayto;
int yearto;
printf("What date were you born? Enter mm/dd/fullyear with no dashes.\n");
scanf("  %d/%d/%d", &month, &day, &year);
printf("What is today's date?\n");
scanf("  %d/%d/%d", &monthto, &dayto, &yearto);

if

(year < 0 && year <= yearto);
year++;
yearcounter++;

if 

(month > 0 && month <= 12);
month++;
monthcounter++;

if 

(monthto > 0 && monthto <= 12);
{
monthto++;
monthcounter2++;
}
extramonths = monthcounter - monthcounter 2;

if

(extramonths <= 0);
{
yearcounter = yearcounter - 1;
}
monthsalive = yearcounter * 12;

printf("You've been alive %d years.\n", yearcounter);
printf("You've been alive about %d months.\n", monthsalive);
return 0;
}

错误是:

  

main.c:41:49:错误:预期';'在数字常量之前   ; extramonths = monthcounter - monthcounter 2;

有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

认为这是什么?

if

(year < 0 && year <= yearto);
year++;
yearcounter++;

(提示:你在if-statment的末尾有一个分号,你的操作周围没有括号。)

你真的想要:

if (year < 0 && year <= yearto)
{
    year++;
    yearcounter++;
}

在这一行:

extramonths = monthcounter - monthcounter 2;

你的意思是2个月的多个月份?您需要 乘法运算符

extramonths = monthcounter - monthcounter * 2;

或许您打算使用变量monthcounter2,在这种情况下,空间只是

extramonths = monthcounter - monthcounter2;