Question
Directions: 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
Directions:
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 late.
Anyway, here is the code, please check:
#include
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]!='\0';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; }
I am having problems with this code crashing after I enter the date although it compiles and runs. I know the program uses an appropriate struct for the date. Although the code includes a custom function that takes a custom struct for the date as a parameter, it does not return a struct with the instructions indicate. Would it be better to have the function return the updated struct instead of having the function print out the date with 7 days added? I was thinking of using scanf("%d/%d/%d", &present.month, &present.day, &present.year);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started