Write a program that asks the user for a date in the format mm/dd/yyyy, using a
ID: 3573625 • Letter: W
Question
Write a program that asks the user for a date in the format mm/dd/yyyy, using a custom struct to store the date information entered in memory. The program should call a custom function that adds seven days to the user's date, passing in the data in and out of the custom function using the custom struct for the dates. The program should then print out the original date entered and the date one week later. As always, be sure to format your C code correctly and include meaningful comments to explain what the code does and how it works.
Explanation / Answer
#include<stdio.h>
struct date//structure declaration
{
int day,month,year;//date,month,year;
};
void addseven(struct date d)//function which adds 7 to present date and prints to stdout
{
int m[] ={31,28,31,30,31,30,31,31,30,31,30,31};//array to keep track of days in month
d.day = d.day+7;//incrementing days
if(d.day>m[d.month-1])//if exceeds monthly days limit then
{
int k=d.day-m[d.month-1];
d.day = k;
d.month = d.month+1;//increment month count
if(d.month>12)//if month exceeded 12
{
d.year =d.year+1;//incrementing year
d.month=1;
}
}
//printing result
printf("Date after one week : %d/%d/%d ",d.month,d.day,d.year);
}
int main()
{
struct date present;//present date
char c[10];
printf("Enter date in format(mm/dd/yyyy) :");
scanf("%s",c);//reading as string
int i=0,k=0,l,s=0;
for(i=0;c[i]!='';i++)//converting to integer... and storing in structure
{
if(c[i]=='/' && k==0)
{
k=1;
present.month = s;//storing month
s=0;
}
else if (c[i]=='/' && k==1)
{
k=2;
present.day = s;//storing day
s=0;
}
else {
l = (int)c[i]-48;
s =s*10+l;}
}
present.year =s;//storing year
//printing present date
printf("present date : %d/%d/%d ",present.month,present.day,present.year);
addseven(present);//function calling
return 0;
}
/*
ouput:-
Enter date in format(mm/dd/yyyy) :12/1/1994
present date : 12/1/1994
Date after one week : 12/8/1994
Process exited normally.
Press any key to continue . . .
Enter date in format(mm/dd/yyyy) :11/28/1994
present date : 11/28/1994
Date after one week : 12/5/1994
Process exited normally.
Press any key to continue . . .
Enter date in format(mm/dd/yyyy) :12/30/1994
present date : 12/30/1994
Date after one week : 1/6/1995
Process exited normally.
Press any key to continue . . .
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.