两段计算年龄的代码,出现问题,大家给帮忙改改,小弟是在不知道问题在哪里,跪谢!
#include <time.h>
#include <stdio.h>
#include <dos.h>
void main()
{
int year,month,day;
struct date{
long year,mon,day;};
struct date *yourbirthd, *nowd;
struct tm{};
struct tm yourbirtht, nowt;
long yourbrith,now;
long resultt;
double result;
printf("\nplease input your birth date (year,month,day):\n");
scanf("%d,%d,%d",&year,&month,&day);
22) getdate(&nowd);
23) gettime(nowt);
yourbirthd->year=year;
yourbirthd->day=day;
yourbirthd->mon=month;
28) yourbirth=dostounix(yourbirthd,yourbirtht);
now=dostounix(nowd,nowt);
resultt=difftime(yourbirthd,nowd);
result=resultt/(24*60*60);
printf("\nYou hava come this world for %d days\n");
}
以上这段代码在VC++中编译出现以下错误,请问如何改正?
--------------------Configuration: 2.2 - Win32 Debug--------------------
Compiling...
2.2.cpp
E:\c\cpp2\2.2.cpp(22) : error C2065: 'getdate' : undeclared identifier
E:\c\cpp2\2.2.cpp(23) : error C2065: 'gettime' : undeclared identifier
E:\c\cpp2\2.2.cpp(28) : error C2065: 'yourbirth' : undeclared identifier
E:\c\cpp2\2.2.cpp(28) : error C2065: 'dostounix' : undeclared identifier
Error executing cl.exe.
2.2.obj - 4 error(s), 0 warning(s)
问题点数:0、回复次数:5Top
1 楼bylmy()回复于 2004-12-01 05:13:26 得分 0
为什么我的这一段代码算出来是个负数??看不出毛病在哪里,请大家帮忙指正,谢谢!!
for(int i=year2+1; i<year1; i++){
if(i%400==0||(i%4==0&&i%100!=0)) /*判断是不是闰年*/
sum3=sum3+366;
else
sum3=sum3+365;
}
其中year2,year1是两个整型变量,用来输入两个年份。如1977,2004
结果出来都是一样的,-864848593,很奇怪。Top
2 楼infini(nebula)回复于 2004-12-01 06:50:20 得分 0
sum3没有初始化吧?Top
3 楼bylmy()回复于 2004-12-01 07:18:30 得分 0
程序修改后,编译后一个错误,说我的birth(打*处)未定义,可是我不是定义了么,怎么回事呢?
#include <time.h>
#include <stdio.h>
#include <dos.h>
void main()
{
int year,month,day;
struct date{
long year,mon,day;
}yourbirthd, nowd;
struct time yourbirtht,nowt;
long brith,now;
double result;
printf("\nplease input your birth date (year,month,day):\n");
scanf("%d,%d,%d",&year,&month,&day);
getdate(&nowd);
gettime(&nowt);
yourbirthd.year=year;
yourbirthd.day=day;
yourbirthd.mon=month;
* birth = dostounix(&yourbirthd,&yourbirtht);
now = dostounix(&nowd,&nowt);
result=difftime(birth,now)/(24*60*60);
printf("\nYou hava come this world for %d days\n", result);
}Top
4 楼bylmy()回复于 2004-12-01 07:21:13 得分 0
是sum3没有初始化,现在已经正确了,谢谢。Top
5 楼bylmy()回复于 2004-12-01 07:55:25 得分 0
现在已经修改后,编译通过,可是运行时,输入生日后,回车没显示结果,是怎么回事?大家给看看是哪里有问题呢?
#include <time.h>
#include <stdio.h>
#include <dos.h>
void main()
{
int year,month,day;
struct date{
long year,mon,day;
}yourbirthd, nowd;
struct time yourbirtht,nowt;
long brith;
long now;
double result;
printf("\nplease input your birth date (year,month,day):\n");
scanf("%d,%d,%d",&year,&month,&day);
getdate(&nowd);
gettime(&nowt);
yourbirthd.year=year;
yourbirthd.day=day;
yourbirthd.mon=month;
birth = dostounix(&yourbirthd,&yourbirtht);
now = dostounix(&nowd,&nowt);
result=difftime(birth,now)/(24*60*60);
printf("\nYou hava come this world for %d days\n", result);
}
Top




