My program intends to achieve this
(A) Write a C function named larger()
that returns the later date of any two dates passed to it. For example, if the dates 10/9/2001 and 11/3/2001 are passed to larger()
, the second date would be returned.
(B) Create the larger() function that was written for (A) in a complete unit. Store the date structure returned by larger() in a separate date structure and display the member values of the returned data structure.
I am working on this problem for my C Language course. I had everything going well (I thought), except that I kept getting "The larger date is: 0/0/0" no matter what I entered. So I started tinkering, and now I cannot get rid of the syntax error, or figure out my 0/0/0 problem. Obviously the dates aren't making it back through. I'm still pretty new at this (and very new at structs), so any help would be awesome!
The line that's giving me the syntax error is near the bottom of main():
DATES result[NUM] = larger(DATES user[NUM]);
The code in full:
#include <stdio.h>
#define NUM 2
struct Dates
{
int month;
int day;
int year;
};
typedef struct Dates DATES;
DATES larger(DATES[NUM]);
DATES more;
int main()
{
DATES user[NUM];
printf("You will enter two dates, and the program will return the larger.
");
while (user[0].month < 1 || user[0].month > 12)
{printf("
Please enter the first month, 1-12: ");
scanf("%d", &user[0].month);}
while (user[0].day < 1 || user[0].day > 31)
{printf("
Please enter the first day, 1-31: ");
scanf("%d", &user[0].day);}
while (user[0].year < 1)
{printf("
Please enter the first year: ");
scanf("%d)", &user[0].year);}
printf("
Date entered: %d/%d/%d.
", user[0].month, user[0].day, user[0].year);
while (user[1].month < 1 || user[1].month > 12)
{printf("
Please enter the first month, 1-12: ");
scanf("%d", &user[1].month);}
while (user[1].day < 1 || user[1].day > 31)
{printf("
Please enter the first day, 1-31: ");
scanf("%d", &user[1].day);}
while (user[1].year < 1)
{printf("
Please enter the first year: ");
scanf("%d)", &user[1].year);}
printf("
Date entered: %d/%d/%d.
", user[1].month, user[1].day, user[1].year);
DATES result[NUM] = larger(DATES user[NUM]); /* Problem here */
printf("The larger date is %d/%d/%d.
", result[0].month, result[0].day, result[0].year);
system("pause");
return 0;
}
DATES larger(DATES more[NUM])
{
int days0;
int days1;
days0 = (more[0].month*31)+(more[0].day)+(more[0].year*365);
days1 = (more[1].month*31)+(more[1].day)+(more[1].year*365);
if (days1 > days0)
{more[0] = more[1];}
return (more[0]);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…