0

The program works well and is correct but doesn't follow requirements to take keyboard input as ONE STRING variable that has to be parsed and the individual values converted to integers. I thought that was what I did, but it appears I'm wrong and I'm just short of ideas.

#include <stdio.h>
#include <stdbool.h>

int main()
{
    int mm, dd,yyyy;

    printf("Welcome to my Date Checker\n");
    printf("Please enter date to test as format (MM/DD/YYYY): ");
    scanf("%d/%d/%d", &mm, &dd, &yyyy);

    // Validates year
    if(yyyy>=1000 && yyyy<=9999)
    {
        // Validates month
        if(mm>=1 && mm<=12)
        {
            // validate days
            if((dd>=1 && dd<=31) &&
            (mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12))
                // Validates if jan, march, may, jul, aug, oct and dec
                // have a max of 31 days
                printf("Date is valid.\n");

            else if((dd>=1 && dd<=30) && (mm==4 || mm==6 || mm==9 || mm==11))
                // Validates if apr, jun, sept, and nov have max of 30 days
                printf("Date is valid.\n");

            else if((dd>=1 && dd<=28) && (mm==2))
                // Validates if feb has max of 28 days
                printf("Date is valid.\n");

            else if(dd==29 && mm==2 && (yyyy%400==0 ||(yyyy%4==0 && yyyy%100!=0)))
                // Validates leap years
                printf("Date is valid.\n");
            else
                printf("Date is invalid.\n");
        }
        else { printf("Month is not valid.\n"); }
    }
    else { printf("Year is not valid.\n"); }

    printf("\nPress any key to continue . . . ");
    getchar();

}

The expected results are correct, just not the process.

Welcome to my Date Checker
Please enter date to test as format (MM/DD/YYYY): 03/25/1999
Date is valid.
  • Use `stftime` function http://man7.org/linux/man-pages/man3/strftime.3.html – lost_in_the_source Apr 30 '19 at 02:00
  • To read the input as a string, either use `"%s"` with `scanf`, or use `fgets` to read the whole line. (Note that `fgets` will put the newline character into the buffer.) – user3386109 Apr 30 '19 at 02:03
  • See [this stackoverflow post](https://stackoverflow.com/questions/22330969/using-fscanf-vs-fgets-and-sscanf) and [this blog post](https://giedrius.blog/2018/06/15/scanf-is-a-newbie-trap-use-fgets-sscanf-instead/). – jamieguinan Apr 30 '19 at 03:05

0 Answers0