Thread: Scanf-ing a date as a string at once

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    43

    Question Scanf-ing a date as a string at once

    I have a problem with acquiring a date from input as a string, this way:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        char day[10], month[2], year[4];
    
        printf("Insert date (mm/dd/yyyy): ");
        scanf("%s/%s/%s", day, month, year);
    
        while (! (strlen(day)==2 && strlen(month)==2 && strlen(year)==4) )
        {
            printf("Invalid format. Retry!\n");
            scanf("%s/%s/%s", day, month, year);
        }
    
        printf("Date inserted: %s/%s/%s\n", year, month, day);
    
        return 0;
    }

    When I enter something from input, it stores all the string in the first variable (i.e. day). How can I get it to work properly without using int in the first place?
    Last edited by sleax; 06-13-2016 at 03:24 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would expect something like this:
    Code:
    if (scanf("%9[^/]/%2[^/]/%4s", day, month, year) != 3)
    {
        /* report the read error or invalid format */
    }
    Notice that I specified the field widths to avoid buffer overflow. This also revealed that your declaration of month and year is wrong, i.e., you should have written:
    Code:
    char day[10], month[3], year[5];
    Otherwise you won't be able to handle double digit months or four digit years.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well both your month and year strings are too short.

    The \0 that scanf will store will always be out of bounds of the arrays.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf that includes / like in a date
    By automagp68 in forum C Programming
    Replies: 17
    Last Post: 01-28-2012, 06:52 PM
  2. string to date
    By xddxogm3 in forum C# Programming
    Replies: 2
    Last Post: 01-22-2008, 07:14 PM
  3. Adding to a string date
    By knutso in forum C Programming
    Replies: 11
    Last Post: 01-20-2004, 11:18 AM
  4. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM
  5. int day to date string?
    By SpuRky in forum C Programming
    Replies: 5
    Last Post: 06-06-2002, 11:49 PM

Tags for this Thread