当前位置: 代码迷 >> C语言 >> 结构{复合字面量问题}
  详细解决方案

结构{复合字面量问题}

热度:855   发布时间:2007-06-11 01:06:42.0
结构{复合字面量问题}

这是一个计算下一天日期程序。问题出现在函数部分复合字面量地方出现语法错误,找了很久,没发现解决办法,请帮我指出一下。
struct date
{
int month;
int year;
int day;
};
struct date update(struct date today)
{
struct date tomorrow;
const int dayspermonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int days;
if(today.year%4==0&&today.year%100!=0||today.year%400==0&&today.month==2)
days=29;
else
days=dayspermonth[today.month-1];
if(today.day!=days)
tomorrow=(struct date){today.month,today.day+1,today.year};

else if (today.month==12)
tomorrow=(struct date){1,1,today.year+1};

else
tomorrow=(struct date){today.monnth+1,1,today.year};
return tomorrow;
}
int main(void)
{
struct date thisdate,nextdate;
struct date update(struct date today);
printf("Input date's date (mm dd yyyy)");
scanf("%i:%i:%i",&thisdate.month,&thisdate.day,&thisdate.year);
nextdate=update(thisdate);
printf("%i:%i:%i",nextdate.month,nextdate.day,nextdate.year);
return 0;
}

搜索更多相关的解决方案: 字面量  int  today  结构  year  

----------------解决方案--------------------------------------------------------

程序代码:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

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

struct date update(struct date today)
{
struct date tomorrow;
const int dayspermonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int days;
if( (today.year%4 == 0 && today.year%100 != 0) || (today.year %400 == 0 && today.month == 2))
{
days=29;
}
else
{
days=dayspermonth[today.month-1];
}
if(today.day!=days)
{
tomorrow=(struct date){today.month,today.day+1,today.year};
}
else
{
if (today.month == 12)
{
tomorrow=(struct date){1,1,today.year+1};
}
else
{
tomorrow=(struct date){today.month+1,1,today.year};
}
}
return tomorrow;
}


int main()
{
struct date thisdate,nextdate;
struct date update(struct date today);
printf(\"Input date's date (mm dd yyyy)\");
scanf(\"%i:%i:%i\",&thisdate.month,&thisdate.day,&thisdate.year);
nextdate = update(thisdate);
printf(\"%i:%i:%i\",nextdate.month,nextdate.day,nextdate.year);
return 0;
}


----------------解决方案--------------------------------------------------------
楼上,先谢你指出的这个month错误,但是还是运行不了。我用的的WIN-TC,不知道是不是编译器问题,你能运行吗?
----------------解决方案--------------------------------------------------------
我可以运行,MINGW....那个括号看见了么?

[此贴子已经被作者于2007-6-11 18:13:56编辑过]



----------------解决方案--------------------------------------------------------

括号看见的,那个都没问题。可有可无,只是可读性好点,我用WIN-TC就报错结构中表达式错误。就那三表达式错误,我想应该是编译器问题了,谢谢你。


----------------解决方案--------------------------------------------------------
回复:(huangfengchu)括号看见的,那个都没问题。可...
看看你定义的结构体的成员变量顺序好像跟下面输出的时候不同哦。呵呵~~
struct date
{
int month;
int day;
int year;
} ;

----------------解决方案--------------------------------------------------------
#include <conio.h>
这个头文件是什么用的?
为哪个函数提供原型还是?
----------------解决方案--------------------------------------------------------

#include <stdio.h>

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

int main()
{

struct date today, tomorrow;
int numbersOfMonth(struct date);

printf("Please enter today's date (month day year):");
scanf("%d%d%d",&today.month,&today.day,&today.year);

tomorrow = (struct date){today.day, today.day, today.year};

if(today.day != numbersOfMonth(today))//非月末判断是否是闰年的2月
{
tomorrow.day = today.day + 1;
}
else if(today.month == 12)//年末
{
tomorrow.day = 1;
tomorrow.month = 1;
tomorrow.year = today.year + 1;
}
else//月末
{
tomorrow.day = 1;
tomorrow.month = today.month + 1;
}
printf("Tomorrow's date is(month/day/year):%d/%d/%.2d\n",
tomorrow.month,tomorrow.day,tomorrow.year%100);
return 0;
}

int numbersOfMonth(struct date today)
{
int days;
bool isLeapYear(struct date);

const int daysPerMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

if(isLeapYear(today) == true && today.month == 2)
days = 29;
else
days = daysPerMonth[today.month - 1];
return days;
}

bool isLeapYear(struct date today)
{
bool leapYearFlag;

if((today.year % 4 ==0 && today.year % 100 != 0) || (today.year % 400 == 0))
leapYearFlag = true;
else
leapYearFlag = false;
return leapYearFlag;

}

为了试一下复合字面量的用法我只用了一个语句,但是没有编译通过,用的是Microsoft Visual C++ 6.0
提示是这样的:
error C2059: syntax error : '{'
error C2143: syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'
都是那一句的错误
请问是编译器的问题,还是别的问题


----------------解决方案--------------------------------------------------------
  相关解决方案