【问题标题】:I got an unknown error on my coding for a C program [closed]我的 C 程序编码出现未知错误 [关闭]
【发布时间】:2013-12-21 14:34:10
【问题描述】:

这是我的代码:

#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 = 月计数器 - 月计数器 2;

有人可以帮帮我吗?

【问题讨论】:

  • 这些if 语句看起来像您在教科书中看到的吗?
  • 编译器正在告诉你错误所在的文件、行和列! monthcounter 和 2 之间缺少某些内容。
  • 这个错误对我来说似乎很有描述性,不知道你为什么说它是“未知的”
  • 那么,extramonths = monthcounter - monthcounter 2; 应该是什么意思?它对我们和编译器一样毫无意义。当无法弄清楚您要做什么时,您如何期望人们“帮助您”?
  • 这是我见过的最丑陋的代码之一。更不用说“bugginess”了。

标签: c error-handling


【解决方案1】:

觉得这是做什么的??

if

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

(提示:if 语句的末尾有一个分号,并且操作周围没有括号。)

你真的想要:

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

在这一行:

extramonths = monthcounter - monthcounter 2;

您的意思是多月计数器乘以 2 吗?为此,您需要一个乘法运算符

extramonths = monthcounter - monthcounter * 2;

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

extramonths = monthcounter - monthcounter2;

【讨论】:

    猜你喜欢
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    相关资源
    最近更新 更多