我完成了代码,并在代码块中无错误地运行了代码,它在命令提示符屏幕上显示了代码中的日期和时间,但是由于某些原因,它将不会更新。有人可以指出我正确的方向还是告诉我我做错了什么。这是我认为已经解决的问题:
编写一个名为clockKeeper的函数,该函数将a作为参数
dateAndTime结构。该函数应调用timeUpdate
函数,如果时间到午夜,则该函数应调用
dateUpdate函数可切换到第二天。有
函数返回更新的dateAndTime结构并将其输出到
终奌站。
#include <stdio.h>
int dt;
struct date
{
int day;
int month;
int year;
};
struct time
{
int seconds;
int minutes;
int hour;
};
struct dateAndTime {
struct date sdate;
struct time stime;
};
struct dateAndTime clockKeeper (struct dateAndTime dt)
{
struct time timeUpdate (struct time now); {
printf ("timeUpdate\n");
return now;
}
struct date dateUpdate (struct date today); {
printf ("dateUpdate\n");
return today;
}
dt.stime = timeUpdate (dt.stime);
if ( dt.stime.hour == 0 && dt.stime.minutes == 0 &&
dt.stime.seconds == 0 )
dt.sdate = dateUpdate (dt.sdate);
return dt;
}
int main (void)
int dt1;
int dt2;
{
struct dateAndTime dt1 = { { 12, 31, 2004 }, { 23, 59, 59 } };
struct dateAndTime dt2 = { { 2, 28, 2008 }, { 23, 59, 58 } };
printf ("Current date and time is %.2i/%.2i/%.2i "
"%.2i:%.2i:%.2i\n",
dt1.sdate.month, dt1.sdate.day, dt1.sdate.year,
dt1.stime.hour,
dt1.stime.minutes, dt1.stime.seconds);
dt1 = clockKeeper (dt1);
printf ("Updated date and time is %.2i/%.2i/%.2i "
"%.2i:%.2i:%.2i\n\n",
dt1.sdate.month, dt1.sdate.day, dt1.sdate.year,
dt1.stime.hour, dt1.stime.minutes, dt1.stime.seconds);
printf ("Current date and time is %.2i/%.2i/%.2i "
"%.2i:%.2i:%.2i\n",
dt2.sdate.month, dt2.sdate.day, dt2.sdate.year,
dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds);
dt2 = clockKeeper (dt2);
printf ("Updated date and time is %.2i/%.2i/%.2i "
"%.2i:%.2i:%.2i\n\n",
dt2.sdate.month, dt2.sdate.day, dt2.sdate.year,
dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds);
printf ("Current date and time is %.2i/%.2i/%.2i "
"%.2i:%.2i:%.2i\n",
dt2.sdate.month, dt2.sdate.day, dt2.sdate.year,
dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds);
dt2 = clockKeeper (dt2);
printf ("Updated date and time is %.2i/%.2i/%.2i "
"%.2i:%.2i:%.2i\n\n",
dt2.sdate.month, dt2.sdate.day, dt2.sdate.year,
dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds);
return 0;
}
最佳答案
似乎您正在尝试在timeUpdate
的定义内定义函数clockKeeper
。编写时(将缩进更改为去混淆):
struct dateAndTime clockKeeper (struct dateAndTime dt)
{
struct time timeUpdate (struct time now);
{ /* This brace may as well be deleted. */
printf ("timeUpdate\n");
return now;
} /* along with this one. */
...
您正在定义函数
clockKeeper
,在其中声明函数timeUpdate和在ClockKeeper中执行的两个语句的存在。 clockKeeper唯一会做的就是这两个语句。如果要定义函数timeUpdate
,则需要将其移到clockKeeper的定义之外,并在分号前删除分号。您的代码可能还有许多其他问题,但这是一个不错的起点。关于c - 我的程序可以编译,但是时间和日期不会更新。有人可以告诉我我要走的路线吗,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34304811/