Date parser in C

0

I was doing tests to do parsers of date and I made this example based on a program that formats through sscanf() with a constant string defined because I did not understand well where is the error:

#include <cstring>
#include <iostream>

int main()
{
   int day, year;
   char weekday[20], month[20], dtm[100];

   printf
    (
     "\n\tDigite Dia Mes a ano em string"
     "\n\tex.(Saturday March 25 1989): "
    );

   scanf("%s80[^\n]",&dtm);

   sscanf(dtm, "%s %s %d  %d", &weekday, &month, &day, &year);

   printf("%s %d, %d = %s\n", month, day, year, weekday);

   return 0;
} 

/*
 Input: 
   Saturday March 25 1989

 Output:

  'l 0, 0 = Saturday 

where Output should be:

March 25 1989 Saturday     * /

    
asked by anonymous 25.07.2017 / 20:28

1 answer

1

This is not exactly a parse date, it is a string with 4 separations between spaces that in thesis the last 2 should be numbers, nothing more.

Actually the error is in the data entry. The default for scanf() is wrong.

#include <stdio.h>

int main() {
    int day, year;
    char weekday[20], month[20], dtm[100];
    printf(
        "\n\tDigite Dia Mes a ano em string"
        "\n\tex.(Saturday March 25 1989): "
    );
    scanf("%[^\n]s80", dtm);
    printf("%s\n", dtm);
    sscanf(dtm, "%s %s %d  %d", weekday, month, &day, &year);
    printf("%s %d, %d = %s\n", month, day, year, weekday);
} 

See running on ideone , and Coding Ground . Also I put GitHub for future reference .

    
25.07.2017 / 21:48