0

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

  • When using `scanf()` and friends, always check the return value to make sure it worked before trying to use the variables it sets. – Shawn Jul 09 '20 at 22:35

1 Answers1

0

You are using wrong format specifier, e.g. %.2i -> %02d:

// ...

printf("Please enter a date formatted as mm/dd/yyyy: ");
scanf("%d/%d/%d", &entered.month, &entered.day, &entered.year);

seven = newDate (entered);

printf("\nThe date you entered is: %02d/%02d/%04d", entered.month, entered.day, entered.year);
printf("\nThe date in seven days will be: %02d/%02d/%04d", seven.month, seven.day, seven.year);

// ...
Kitsu
  • 3,166
  • 14
  • 28
  • i is almost always the wrong format specifier for getting number input unless the user is expected to need entering octal & hexadecimal since input with leading zero is treated as octal (eg, 09 will be parsed as 0 since octal only have 0-7) https://stackoverflow.com/questions/1893490/what-is-the-difference-between-conversion-specifiers-i-and-d-in-formatted-io-f – Martheen Jul 09 '20 at 23:04
  • I updated the code, but when I enter any date I am always getting the print out date of 02/16/0000 and the other date as 02/23/0000. So I am still uncertain as to what is missing. Can you see the reason I am not getting the expected results by chance? – Annette Kitz Jul 09 '20 at 23:09
  • So, the program isn't reading the date entered by the user, it is using today date of 09 and added 7 to it and then 7 more. I am just not sure how to fix it. – Annette Kitz Jul 09 '20 at 23:21
  • Sorry, I'm not sure I got your point. At [ideone](https://ideone.com/E2gSx2) your example works as expected (look at *stdout* section). – Kitsu Jul 10 '20 at 05:37