Write a program that asks the user for a date in the format mm/dd/yyyy, using a
ID: 3579621 • 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
Please refer below code
#include<stdlib.h>
#include<stdio.h>
#include <stdbool.h>
struct date
{
int month,date,year;
};
bool modify_date(struct date *temp)
{
switch(temp->month) //checking with respect to month
{
//1,3,5,7,8,10 months have last date as 31
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
if(temp->date > 31)
{
printf("Invalid date ");
return false;
}
else
{
temp->date += 7;
if(temp->date > 31)
{
temp->month++;
temp->date = temp->date % 31;
}
}
break;
//february lastdate depends upon whether entered year is leap year.Leap year is having last date 29 otherwise 28
case 2:
if((temp->year % 4 ==0) || (temp->year % 400 == 0))
{
if(temp->date > 29)
{
printf("Invalid date ");
return false;
}
else
{
temp->date += 7;
if(temp->date > 29)
{
temp->month++;
temp->date = temp->date % 29;
}
}
}
else
{
if(temp->date > 28)
{
printf("Invalid date ");
return false;
}
else
{
temp->date += 7;
if(temp->date > 28)
{
temp->month++;
temp->date = temp->date % 28;
}
}
}
break;
case 4:
case 6:
case 9:
case 11:
if(temp->date > 30)
{
printf("Invalid date ");
return false;
}
else
{
temp->date += 7;
if(temp->date > 30)
{
temp->month++;
temp->date = temp->date % 30;
}
}
break;
//for december month year and month both changes
case 12:
if(temp->date > 31)
{
printf("Invalid date ");
return false;
}
else
{
temp->date += 7;
if(temp->date > 31)
{
temp->month++;
temp->year++;
temp->month = temp->month % 12;
temp->date = temp->date % 31;
}
}
break;
default:
printf("Invalid Month ");
return false;
break;
}
return true;
}
int main()
{
char ddmmyyyy[20];
struct date data;
printf("Enter date in format mm/dd/yyyy : ");
scanf("%s", ddmmyyyy);
sscanf(ddmmyyyy, "%2d/%2d/%4d", &data.month, &data.date,&data.year);
printf("Entered date is : %d/%d/%d ",data.month,data.date,data.year);
if(modify_date(&data))
printf("Modified date is : %d/%d/%d ",data.month,data.date,data.year);
return 0;
}
Please refer below output for reference
Enter date in format mm/dd/yyyy : 12/30/2016
Entered date is : 12/30/2016
Modified date is : 1/6/2017
Process returned 0 (0x0) execution time : 10.631 s
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.