I am working on a program that a user enters a date and then the program adds 7 days and prints out the date entered and then prints out the date with seven days added. I am getting the month and date correctly formatted and it appears that it is adding the seven days. However, the year is printing as four zeros and I am not quite sure what I am missing. Below is a screen show of the results and the code follows. Can someone let me know what is needed to print out the year please?
Result: Please enter a date formatted as mm/dd/yyyy: 02/02/2000
The date you entered is: 02/16/0000 The date in seven days will be: 02/23/0000 Process returned 0 (0x0) execution time : 7.071 s Press any key to continue.
#include <stdio.h>
#include <stdbool.h>
//This is a global definition of struct for date so it can be utilized for any function within the program.
struct date
{
int month;
int day;
int year;
};
//This will add the seven days to the date entered by the user
struct date newDate (struct date today)
{
struct date week;
int daysOfMonth (struct date d);
if(today.day != daysOfMonth(today))
{
week.day = today.day+7;
week.month = today.month;
week.year = today.year;
}
else if(today.month == 12)//end of month
{
week.day = 1;
week.month = today.month = 1;
week.year = today.year +1;
}
else //end of year
{
week.day = 1;
week.month = today.month+1;
week.year = today.year;
}
return week;
}
//Function to find the number of days in a month
int daysOfMonth (struct date d)
{
//Declare variables to be used within function
int days;
bool flagLeapYear (struct date d);
const int daysOfMonth [12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (flagLeapYear (d) == true && d.month == 2)
days = 29;
else
days = daysOfMonth[d.month - 1];
return days;
}
//Function to determine if it is a leap year
bool flagLeapYear (struct date d)
{
bool flagLeapYear;
if ((d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0)
flagLeapYear = true; //It is a leap year
else
flagLeapYear = false; // Not a leap year
return flagLeapYear;
}
int main(void)
{
struct date newDate (struct date today);
struct date entered, seven;
printf("Please enter a date formatted as mm/dd/yyyy: ");
scanf(" %i%i%i", &entered.month, &entered.day, &entered.year);
seven = newDate (entered);
printf("\nThe date you entered is: %.2i/%.2i/%.4i", entered.month, entered.day, entered.year);
printf("\nThe date in seven days will be: %.2i/%.2i/%.4i", seven.month, seven.day, seven.year);
return 0;
}
Thanks, Annette